import { DtIconClose as I } from "@dialpad/dialtone-icons/vue3";
import D from "../../common/mixins/modal.js";
import { MODAL_BANNER_KINDS as y, MODAL_SIZE_MODIFIERS as b, MODAL_KIND_MODIFIERS as E } from "./modal-constants.js";
import { disableRootScrolling as O, returnFirstEl as g, enableRootScrolling as B, hasSlotContent as F, getUniqueString as M } from "../../common/utils/index.js";
import { EVENT_KEYNAMES as d } from "../../common/constants/index.js";
import A from "../../shared/sr_only_close_button.js";
import { DialtoneLocalization as N } from "../../localization/index.js";
import { resolveComponent as r, createBlock as u, openBlock as a, Teleport as R, createVNode as h, mergeProps as L, toHandlers as v, withCtx as f, createElementBlock as n, createCommentVNode as S, normalizeClass as c, renderSlot as m, createTextVNode as z, toDisplayString as _, Transition as K, withDirectives as q, createElementVNode as j, vShow as H } from "vue";
import { _ as V } from "../../_plugin-vue_export-helper-CHgC5LLL.js";
import P from "../button/button.js";
import U from "../lazy-show/lazy-show.js";
import { NOTICE_KINDS as W } from "../notice/notice-constants.js";
const Z = {
  compatConfig: { MODE: 3 },
  name: "DtModal",
  components: {
    DtLazyShow: U,
    DtButton: P,
    DtIconClose: I,
    SrOnlyCloseButton: A
  },
  mixins: [D],
  props: {
    /**
     * Body text to display as the modal's main content.
     */
    copy: {
      type: String,
      default: ""
    },
    /**
     * Id to use for the dialog's aria-describedby.
     * Recommended only if the dialog content itself isn't enough to give full context,
     * as screen readers should recite the dialog contents by default before any aria-description.
     */
    describedById: {
      type: String,
      default: ""
    },
    /**
     * Id to use for the dialog's aria-labelledby.
     */
    labelledById: {
      type: String,
      default: function() {
        return M();
      }
    },
    /**
     * Whether the modal should be shown.
     * Parent component can sync on this value to control the modal's visibility.
     * @values true, false
     */
    show: {
      type: Boolean,
      default: !1
    },
    /**
     * Title text to display in the modal header.
     */
    title: {
      type: String,
      default: ""
    },
    /**
     * Title text to display in the modal banner.
     */
    bannerTitle: {
      type: String,
      default: ""
    },
    /**
     * The theme of the modal. kind - default or danger,
     * @values default, danger
     */
    kind: {
      type: String,
      default: "default",
      validator: (e) => Object.keys(E).includes(e)
    },
    /**
     * The size of the modal. size - default or full,
     * @values default, full
     */
    size: {
      type: String,
      default: "default",
      validator: (e) => Object.keys(b).includes(e)
    },
    /**
     * Additional class name for the root modal element.
     * Can accept String, Object, and Array, i.e. has the
     * same API as Vue's built-in handling of the class attribute.
     */
    modalClass: {
      type: [String, Object, Array],
      default: ""
    },
    /**
     * Additional class name for the dialog element within the modal.
     * Can accept String, Object, and Array, i.e. has the
     * same API as Vue's built-in handling of the class attribute.
     */
    dialogClass: {
      type: [String, Object, Array],
      default: ""
    },
    /**
     * Additional class name for the content element within the modal.
     * Can accept String, Object, and Array, i.e. has the
     * same API as Vue's built-in handling of the class attribute.
     */
    contentClass: {
      type: [String, Object, Array],
      default: ""
    },
    /**
     * Sets the color of the banner.
     * @values base, error, info, success, warning
     */
    bannerKind: {
      type: String,
      default: "warning",
      validate(e) {
        return W.includes(e);
      }
    },
    /**
     * Additional class name for the banner element within the modal.
     * Can accept String, Object, and Array, i.e. has the
     * same API as Vue's built-in handling of the class attribute.
     */
    bannerClass: {
      type: [String, Object, Array],
      default: ""
    },
    /**
     * Hides the close button on the modal
     * @values true, false
     */
    hideClose: {
      type: Boolean,
      default: !1
    },
    /**
     * Whether the modal will close when you click outside of the dialog on the overlay.
     * @values true, false
     */
    closeOnClick: {
      type: Boolean,
      default: !0
    },
    /**
     * Scrollable modal that allows scroll the modal content keeping the header and footer fixed
     * @values true, false
     */
    fixedHeaderFooter: {
      type: Boolean,
      default: !0
    },
    /**
     * The element that is focused when the modal is opened. This can be an
     * HTMLElement within the modal, a string starting with '#' which will
     * find the element by ID. 'first' which will automatically focus
     * the first element, or 'dialog' which will focus the dialog window itself.
     * If the dialog is modal this prop cannot be 'none'.
     */
    initialFocusElement: {
      type: [String, HTMLElement],
      default: "first",
      validator: (e) => e === "first" || e instanceof HTMLElement || e.startsWith("#")
    },
    /**
     * A CSS selector string for the element to portal the modal to. If not provided, the modal will be rendered in its default location.
     */
    appendTo: {
      type: String,
      default: void 0
    }
  },
  emits: [
    /**
     * Native button click event
     *
     * @event click
     * @type {PointerEvent | KeyboardEvent}
     */
    "click",
    /**
     * Native keydown event
     *
     * @event keydown
     * @type {KeyboardEvent}
     */
    "keydown",
    /**
     * The modal will emit a "false" boolean value for this event when the user performs a modal-closing action.
     * Parent components can sync on this value to create a 2-way binding to control modal visibility.
     *
     * @event update:show
     * @type {Boolean}
     */
    "update:show"
  ],
  data() {
    return {
      MODAL_KIND_MODIFIERS: E,
      MODAL_SIZE_MODIFIERS: b,
      MODAL_BANNER_KINDS: y,
      EVENT_KEYNAMES: d,
      hasSlotContent: F,
      i18n: new N()
    };
  },
  computed: {
    modalListeners() {
      return {
        click: (e) => {
          this.closeOnClick && e.target === e.currentTarget ? this.close() : this.show && e.target !== e.currentTarget && this.handleModalClick(e), this.$emit("click", e);
        },
        keydown: (e) => {
          switch (e.code) {
            case d.esc:
            case d.escape:
              this.close();
              break;
            case d.tab:
              this.trapFocus(e);
              break;
          }
          this.$emit("keydown", e);
        },
        "after-enter": async () => {
          this.$emit("update:show", !0), await this.setFocusAfterTransition();
        },
        focusin: (e) => {
          var t;
          const o = ((t = this.$refs.modalRoot) == null ? void 0 : t.$el) || this.$el;
          this.show && o && !o.contains(e.target) && (e.preventDefault(), this.focusFirstElement(o));
        }
      };
    },
    open() {
      return `${!this.show}`;
    },
    hasFooterSlot() {
      return !!this.$slots.footer;
    },
    bannerKindClass() {
      return y[this.bannerKind];
    },
    closeButtonTitle() {
      return this.i18n.$t("DIALTONE_CLOSE_BUTTON");
    }
  },
  watch: {
    show: {
      handler(e) {
        var o, t, i;
        if (e) {
          this.previousActiveElement = document.activeElement;
          const l = ((o = this.$refs.modalRoot) == null ? void 0 : o.$el) || this.$el;
          O(g(l).getRootNode().host);
        } else {
          const l = ((t = this.$refs.modalRoot) == null ? void 0 : t.$el) || this.$el;
          B(g(l).getRootNode().host), (i = this.previousActiveElement) == null || i.focus(), this.previousActiveElement = null;
        }
      }
    }
  },
  methods: {
    close() {
      this.$emit("update:show", !1);
    },
    async setFocusAfterTransition() {
      var o;
      const e = ((o = this.$refs.modalRoot) == null ? void 0 : o.$el) || this.$el;
      this.initialFocusElement === "first" ? await this.focusFirstElement(e) : this.initialFocusElement.startsWith("#") ? await this.focusElementById(this.initialFocusElement) : this.initialFocusElement instanceof HTMLElement && this.initialFocusElement.focus();
    },
    trapFocus(e) {
      var o;
      if (this.show) {
        const t = ((o = this.$refs.modalRoot) == null ? void 0 : o.$el) || this.$el;
        this.focusTrappedTabPress(e, t);
      }
    },
    handleModalClick(e) {
      var l;
      const o = e.target, t = ((l = this.$refs.modalRoot) == null ? void 0 : l.$el) || this.$el, i = this._getFocusableElements(t);
      i.length && !i.includes(o) && (i.includes(document.activeElement) || this.focusFirstElement(t));
    }
  }
}, x = ["aria-describedby", "aria-labelledby"], Y = ["id"], $ = ["id"], G = {
  key: 4,
  class: "d-modal__footer"
};
function J(e, o, t, i, l, s) {
  const C = r("sr-only-close-button"), k = r("dt-icon-close"), w = r("dt-button"), p = r("dt-lazy-show");
  return a(), u(R, {
    disabled: !t.appendTo,
    to: t.appendTo
  }, [
    h(p, L({
      ref: "modalRoot",
      transition: "d-zoom",
      show: t.show,
      class: [
        "d-modal",
        l.MODAL_KIND_MODIFIERS[t.kind],
        l.MODAL_SIZE_MODIFIERS[t.size],
        t.modalClass
      ],
      "data-qa": "dt-modal",
      "aria-hidden": s.open
    }, v(s.modalListeners)), {
      default: f(() => [
        t.show && (l.hasSlotContent(e.$slots.banner) || t.bannerTitle) ? (a(), n("div", {
          key: 0,
          "data-qa": "dt-modal-banner",
          class: c([
            "d-modal__banner",
            t.bannerClass,
            s.bannerKindClass
          ])
        }, [
          m(e.$slots, "banner", {}, () => [
            z(_(t.bannerTitle), 1)
          ])
        ], 2)) : S("", !0),
        h(K, {
          appear: "",
          name: "d-modal__dialog"
        }, {
          default: f(() => [
            q(j("div", {
              class: c([
                "d-modal__dialog",
                { "d-modal__dialog--scrollable": t.fixedHeaderFooter },
                t.dialogClass
              ]),
              role: "dialog",
              "aria-modal": "true",
              "aria-describedby": t.describedById,
              "aria-labelledby": t.labelledById
            }, [
              l.hasSlotContent(e.$slots.header) ? (a(), n("div", {
                key: 0,
                id: t.labelledById,
                class: "d-modal__header",
                "data-qa": "dt-modal-title"
              }, [
                m(e.$slots, "header")
              ], 8, Y)) : (a(), n("h2", {
                key: 1,
                id: t.labelledById,
                class: "d-modal__header",
                "data-qa": "dt-modal-title"
              }, _(t.title), 9, $)),
              l.hasSlotContent(e.$slots.default) ? (a(), n("div", {
                key: 2,
                class: c([
                  "d-modal__content",
                  t.contentClass
                ]),
                "data-qa": "dt-modal-copy"
              }, [
                m(e.$slots, "default")
              ], 2)) : (a(), n("p", {
                key: 3,
                class: c([
                  "d-modal__content",
                  t.contentClass
                ]),
                "data-qa": "dt-modal-copy"
              }, _(t.copy), 3)),
              s.hasFooterSlot ? (a(), n("footer", G, [
                m(e.$slots, "footer")
              ])) : S("", !0),
              t.hideClose ? (a(), u(C, {
                key: 5,
                onClose: s.close
              }, null, 8, ["onClose"])) : (a(), u(w, {
                key: 6,
                class: "d-modal__close",
                "data-qa": "dt-modal-close-button",
                size: "md",
                kind: "muted",
                importance: "clear",
                "aria-label": s.closeButtonTitle,
                title: s.closeButtonTitle,
                onClick: s.close
              }, {
                icon: f(({ iconSize: T }) => [
                  h(k, { size: T }, null, 8, ["size"])
                ]),
                _: 1
              }, 8, ["aria-label", "title", "onClick"]))
            ], 10, x), [
              [H, t.show]
            ])
          ]),
          _: 3
        })
      ]),
      _: 3
    }, 16, ["show", "class", "aria-hidden"])
  ], 8, ["disabled", "to"]);
}
const ce = /* @__PURE__ */ V(Z, [["render", J]]);
export {
  ce as default
};
//# sourceMappingURL=modal.js.map
