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 { findParent } from '../../../../utils/ui';
import { BodyPositionedWrapper } from './body-positioned-wrapper.abstract';
import { MapToComponentField as MapToSlimmerField } from 'vega-slimmer/core';
/**
 * This class is is used to get vega-modal element as a container and store the element appender element to slot.
 *
 * @property {HTMLElement} showConfig the show config
 */
export class VegaModalWrapper extends BodyPositionedWrapper {
  /**
   * The canRetrieve function just determines whether the target of showConfig has the `vega-modal` element. If not, search upwards layer by layer.
   *
   * @returns {boolean} the boolean type.
   */
  canRetrieve() {
    return super.canRetrieve() && !!findParent(this.showConfig.target, 'vega-modal');
  }
  /**
   * The retrieve function get the slot position of vega-modal element.
   *
   * @returns {HTMLElement} the element.
   */
  retrieve() {
    return this.getVegaModalShowConfigContainer();
  }
  /**
   * This function `getVegaModalContentWithoutScroll` determines whether vega-modal has a slot whose name is `content` or `modal-content`. If there is a component rendered in the modal, the elementAppender added to the corresponding slot attribute is added. If not, the elementAppender is not directly appended to the root directory of vega-modal.
   *
   * @param {Nullable<HTMLElement>} parentModal The parentModal is vega-modal element.
   * @param {Nullable<HTMLElement>} element The parentModal is vega-modal element.
   */
  setElementSlot(parentModal, element) {
    var _a;
    const slotName = (_a = Array.from(parentModal.children)
      .find((item) => {
      const slotName = item.getAttribute('slot');
      return slotName && ['content', 'modal-content'].includes(slotName);
    })) === null || _a === void 0 ? void 0 : _a.getAttribute('slot');
    if (slotName) {
      element.setAttribute('slot', slotName);
    }
  }
  /**
   * The function "getShowConfigContainer" returns the appended element container. If the modal content can be scrolled, the container is the div element that wraps the append element. Otherwise, the container is the modal dialog itself.
   *
   * @returns {HTMLElement} the `scrollContainer` element.
   */
  getVegaModalShowConfigContainer() {
    const parentModal = findParent(this.showConfig.target, 'vega-modal');
    const modalContentScrollable = parentModal.shadowRoot.querySelector('.vega-modal-content-with-scroll');
    const modalDialog = parentModal.shadowRoot.querySelector('.modal-dialog');
    this.setElementSlot(parentModal, this.showConfig.appendableElement);
    parentModal.append(this.showConfig.appendableElement);
    return modalContentScrollable ? modalContentScrollable : modalDialog;
  }
}
__decorate([
  MapToSlimmerField({ writable: true })
], VegaModalWrapper.prototype, "showConfig", void 0);
