<?php

declare(strict_types=1);

namespace Drupal\Tests\acquia_dam\FunctionalJavascript;

use Drupal\Tests\ckeditor5\Traits\CKEditor5TestTrait;

/**
 * Tests Gin sticky form actions compatibility with Acquia DAM dialogs.
 *
 * When the Navigation module is active, Gin force-enables sticky action
 * buttons for all forms with actions. The Acquia DAM embed form uses
 * dialogType: 'ajax', which bypasses Gin's isModalOrOffcanvas() guard.
 * This caused Gin's more_actions.js to overwrite the parent content form's
 * sticky submit button attributes with data from the AJAX-loaded DAM form.
 *
 * hook_gin_ignore_sticky_form_actions() prevents this by excluding Acquia
 * DAM form IDs from Gin's sticky button processing.
 *
 * @group acquia_dam_functional_javascript_media_library_integration
 */
final class GinStickyFormActionsTest extends AcquiaDamWebDriverTestBase {

  use CKEditor5TestTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'gin';

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'ckeditor5',
    'node',
    'field_ui',
    'acquia_dam',
    'acquia_dam_test',
    'navigation',
    'dblog',
  ];

  /**
   * Tests that the sticky button is preserved after media library interaction.
   *
   * Without hook_gin_ignore_sticky_form_actions, the sticky Save button's
   * form attribute gets overwritten by Gin's more_actions.js processing of
   * the AJAX-loaded DAM embed form, causing the node submission to fail.
   */
  public function testNodeSavesAfterMediaLibraryDialog(): void {
    $user = $this->drupalCreateUser([
      'use text format test_format',
      'access media overview',
      'create page content',
      'edit any page content',
      'access navigation',
    ]);
    $this->drupalLogin($user);
    $this->grantSiteRegistrationToken();
    $this->grantCurrentUserDamToken();

    $this->drupalGet('/node/add/page');
    $this->getSession()->getPage()->fillField('Title', 'Gin sticky test');

    // Verify Gin's sticky action bar is rendered for the content form.
    $this->assertSession()->elementExists('css', '.gin-sticky-form-actions');

    // Capture the form attribute before dialog interaction.
    $before_form = $this->getSession()->evaluateScript(
      "return document.querySelector('[data-drupal-selector=\"gin-sticky-edit-submit\"]')?.getAttribute('form')"
    );
    $this->assertNotNull($before_form, 'Sticky Save button must have a form attribute before dialog');

    $this->waitForEditor();
    $this->pressEditorButton('Insert Media');
    $assert_session = $this->assertSession();
    $assert_session->assertWaitOnAjaxRequest();
    $this->assertSession()->waitForElement('css', '#drupal-modal');

    // Using the editor path ($is_editor=TRUE) causes the DAM embed form
    // dialog to open, which is what triggers the Gin sticky button bug.
    $this->selectAndInsertAsset('0324b0b2-5293-4aa0-b0aa-c85b003395e2', 'PDF', 'inline_view');

    // Verify the form attribute was NOT overwritten.
    $after_form = $this->getSession()->evaluateScript(
      "return document.querySelector('[data-drupal-selector=\"gin-sticky-edit-submit\"]')?.getAttribute('form')"
    );
    $this->assertSame(
      $before_form,
      $after_form,
      'Sticky Save button form attribute must not change after AJAX-loaded DAM form is processed'
    );

    // Save the node. If the sticky button was corrupted, this would fail.
    $this->getSession()->getPage()->pressButton('Save');
    $this->assertSession()->waitForText('Gin sticky test', 20000);
    $this->assertSession()->pageTextContains('Gin sticky test');
  }

}
