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, VegaSlimmer } from 'vega-slimmer/core';
/**
 * Modal backdrop controller
 */
export class VegaModalBackdropController extends VegaSlimmer {
  /**
   * Get the backdropRef element or null.
   *
   * @returns {HTMLElement | null} - The backdropRef.
   */
  getBackdropRef() {
    return this.backdropRef;
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-modal-backdrop-color-default-e2e}
   * @see{module:vega-modal-backdrop-color-semi-e2e}
   * @see{module:vega-modal-backdrop-color-none-e2e}
   * @see{module:vega-modal-default-e2e}
   */
  /* istanbul ignore next */
  /**
   * Set backdrop of modal.
   */
  setBackdrop() {
    var _a;
    if (this.backdrop === 'none')
      return;
    // eslint-disable-next-line no-restricted-globals
    let backdrop = document.querySelector(`vega-backdrop.${VegaModalBackdropController.modalBackdropClass}`);
    if (!backdrop) {
      // eslint-disable-next-line no-restricted-globals
      backdrop = document.createElement('vega-backdrop');
      backdrop.classList.add(VegaModalBackdropController.modalBackdropClass);
      backdrop.color = VegaModalBackdropController.backdropColorMap[this.backdropColor];
      backdrop.visible = true;
      // Append backdrop as a sibling of the modal host so it shares the same stacking context.
      // - When vega-modal is in document.body (normal case), parentElement is document.body.
      // - When vega-modal is inside a third-party dialog (e.g. Bootstrap modal), parentElement is the dialog container — keeping the backdrop inside the focus-trap root.
      // - Falls back to document.body when parentElement is null (e.g. VegaDialog creates the vega-modal element before appending it to the DOM).
      // eslint-disable-next-line no-restricted-globals
      ((_a = this.host.parentElement) !== null && _a !== void 0 ? _a : document.body).append(backdrop);
    }
    this.backdropRef = backdrop;
  }
  /**
   * Removes the backdrop element if it exists.
   */
  removeBackdrop() {
    this.backdropRef && this.backdropRef.remove();
  }
}
VegaModalBackdropController.modalBackdropClass = 'vega-modal-backdrop';
VegaModalBackdropController.backdropColorMap = {
  default: 'bg-backdrop-modal',
  semi: 'bg-backdrop-popover',
  none: 'bg-transparent',
};
__decorate([
  MapToComponentField()
], VegaModalBackdropController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaModalBackdropController.prototype, "backdrop", void 0);
__decorate([
  MapToComponentField()
], VegaModalBackdropController.prototype, "backdropColor", void 0);
