var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField as MapToSlimmerField } from 'vega-slimmer/core';
import { findParent } from '../../../../utils/ui';
import { BodyPositionedWrapper } from './body-positioned-wrapper.abstract';
/**
 * Appends the popover content box inside the nearest third-party dialog/modal (e.g. Bootstrap,
 * MUI) so that their focus trap does not block focus on vega-popover content.
 *
 * Detection strategy:
 * 1. Walk up from the target to find `dialog` or `[aria-modal="true"]`.
 * 2. Skip Vega-owned elements (tag starts with `VEGA-`).
 * 3. Locate the focus-trap root via `closest('[tabindex="-1"]')`.
 */
export class ThirdPartyDialogWrapper extends BodyPositionedWrapper {
  /**
   * Returns true when the target lives inside a third-party dialog.
   *
   * NOTE: Does NOT call super.canRetrieve() — must activate even when isScreenPosition=true
   * (e.g. image editor popover) so the content box stays inside the focus-trap root.
   *
   * @returns {boolean} Whether the target is inside a third-party dialog.
   */
  canRetrieve() {
    this.focusTrapRoot = this.findFocusTrapRoot(this.showConfig.target);
    return !!this.focusTrapRoot;
  }
  /**
   * Appends the popover content box inside the focus-trap root.
   *
   * @returns {HTMLElement} The focus-trap root element.
   */
  retrieve() {
    const root = this.focusTrapRoot;
    this.focusTrapRoot = null;
    if (this.showConfig.appendableElement.parentElement !== root) {
      root.append(this.showConfig.appendableElement);
    }
    return root;
  }
  /**
   * Finds the focus-trap root of the nearest third-party dialog ancestor.
   * Returns null if not inside a third-party dialog or if the dialog is Vega-owned.
   *
   * @param {Nullable<HTMLElement>} el - The element to start searching from.
   * @returns {Nullable<HTMLElement>} The focus-trap root, or null.
   */
  findFocusTrapRoot(el) {
    var _a;
    const dialog = findParent(el, 'dialog, [aria-modal="true"]');
    if (!dialog || this.isVegaOwned(dialog))
      return null;
    // closest() also matches the element itself — needed when [aria-modal] and [tabindex="-1"]
    // are on the same node (e.g. Bootstrap 5 outer .modal div).
    return (_a = dialog.closest('[tabindex="-1"]')) !== null && _a !== void 0 ? _a : dialog;
  }
  /**
   * Returns true when the element is a Vega custom element (tag starts with `VEGA-`).
   *
   * @param {HTMLElement} el - The element to check.
   * @returns {boolean} True if the element is Vega-owned.
   */
  isVegaOwned(el) {
    return el.tagName.startsWith('VEGA-');
  }
}
__decorate([
  MapToSlimmerField()
], ThirdPartyDialogWrapper.prototype, "showConfig", void 0);
