import { DtIconClose as D } from "@dialpad/dialtone-icons/vue3";
import O from "../../common/mixins/modal.js";
import { MODAL_BANNER_KINDS as f, MODAL_SIZE_MODIFIERS as _, MODAL_KIND_MODIFIERS as y } from "./modal-constants.js";
import { disableRootScrolling as p, returnFirstEl as b, enableRootScrolling as T, hasSlotContent as B, getUniqueString as F } from "../../common/utils/index.js";
import { EVENT_KEYNAMES as n } from "../../common/constants/index.js";
import M from "../../shared/sr_only_close_button.js";
import { DialtoneLocalization as A } from "../../localization/index.js";
import { resolveComponent as d, createBlock as m, openBlock as o, mergeProps as N, toHandlers as L, withCtx as u, createElementBlock as a, createCommentVNode as g, createVNode as E, normalizeClass as r, renderSlot as c, createTextVNode as v, toDisplayString as h, Transition as z, withDirectives as K, createElementVNode as R, vShow as q } from "vue";
import { _ as j } from "../../_plugin-vue_export-helper-CHgC5LLL.js";
import H from "../button/button.js";
import V from "../lazy-show/lazy-show.js";
import { NOTICE_KINDS as P } from "../notice/notice-constants.js";
const U = {
  compatConfig: { MODE: 3 },
  name: "DtModal",
  components: {
    DtLazyShow: V,
    DtButton: H,
    DtIconClose: D,
    SrOnlyCloseButton: M
  },
  mixins: [O],
  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 F();
      }
    },
    /**
     * 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(y).includes(e)
    },
    /**
     * The size of the modal. size - default or full,
     * @values default, full
     */
    size: {
      type: String,
      default: "default",
      validator: (e) => Object.keys(_).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 P.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("#")
    }
  },
  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: y,
      MODAL_SIZE_MODIFIERS: _,
      MODAL_BANNER_KINDS: f,
      EVENT_KEYNAMES: n,
      hasSlotContent: B,
      i18n: new A()
    };
  },
  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 n.esc:
            case n.escape:
              this.close();
              break;
            case n.tab:
              this.trapFocus(e);
              break;
          }
          this.$emit("keydown", e);
        },
        "after-enter": async () => {
          this.$emit("update:show", !0), await this.setFocusAfterTransition();
        },
        focusin: (e) => {
          this.show && !this.$el.contains(e.target) && (e.preventDefault(), this.focusFirstElement());
        }
      };
    },
    open() {
      return `${!this.show}`;
    },
    hasFooterSlot() {
      return !!this.$slots.footer;
    },
    bannerKindClass() {
      return f[this.bannerKind];
    },
    closeButtonTitle() {
      return this.i18n.$t("DIALTONE_CLOSE_BUTTON");
    }
  },
  watch: {
    show: {
      handler(e) {
        var s;
        e ? (this.previousActiveElement = document.activeElement, p(b(this.$el).getRootNode().host)) : (T(b(this.$el).getRootNode().host), (s = this.previousActiveElement) == null || s.focus(), this.previousActiveElement = null);
      }
    }
  },
  methods: {
    close() {
      this.$emit("update:show", !1);
    },
    async setFocusAfterTransition() {
      this.initialFocusElement === "first" ? await this.focusFirstElement() : this.initialFocusElement.startsWith("#") ? await this.focusElementById(this.initialFocusElement) : this.initialFocusElement instanceof HTMLElement && this.initialFocusElement.focus();
    },
    trapFocus(e) {
      this.show && this.focusTrappedTabPress(e);
    },
    handleModalClick(e) {
      const s = e.target, t = this._getFocusableElements();
      t.length && !t.includes(s) && (t.includes(document.activeElement) || this.focusFirstElement());
    }
  }
}, W = ["aria-describedby", "aria-labelledby"], Z = ["id"], x = ["id"], Y = {
  key: 4,
  class: "d-modal__footer"
};
function G(e, s, t, J, i, l) {
  const S = d("sr-only-close-button"), C = d("dt-icon-close"), k = d("dt-button"), w = d("dt-lazy-show");
  return o(), m(w, N({
    transition: "d-zoom",
    show: t.show,
    class: [
      "d-modal",
      i.MODAL_KIND_MODIFIERS[t.kind],
      i.MODAL_SIZE_MODIFIERS[t.size],
      t.modalClass
    ],
    "data-qa": "dt-modal",
    "aria-hidden": l.open
  }, L(l.modalListeners)), {
    default: u(() => [
      t.show && (i.hasSlotContent(e.$slots.banner) || t.bannerTitle) ? (o(), a("div", {
        key: 0,
        "data-qa": "dt-modal-banner",
        class: r([
          "d-modal__banner",
          t.bannerClass,
          l.bannerKindClass
        ])
      }, [
        c(e.$slots, "banner", {}, () => [
          v(h(t.bannerTitle), 1)
        ])
      ], 2)) : g("", !0),
      E(z, {
        appear: "",
        name: "d-modal__dialog"
      }, {
        default: u(() => [
          K(R("div", {
            class: r([
              "d-modal__dialog",
              { "d-modal__dialog--scrollable": t.fixedHeaderFooter },
              t.dialogClass
            ]),
            role: "dialog",
            "aria-modal": "true",
            "aria-describedby": t.describedById,
            "aria-labelledby": t.labelledById
          }, [
            i.hasSlotContent(e.$slots.header) ? (o(), a("div", {
              key: 0,
              id: t.labelledById,
              class: "d-modal__header",
              "data-qa": "dt-modal-title"
            }, [
              c(e.$slots, "header")
            ], 8, Z)) : (o(), a("h2", {
              key: 1,
              id: t.labelledById,
              class: "d-modal__header",
              "data-qa": "dt-modal-title"
            }, h(t.title), 9, x)),
            i.hasSlotContent(e.$slots.default) ? (o(), a("div", {
              key: 2,
              class: r([
                "d-modal__content",
                t.contentClass
              ]),
              "data-qa": "dt-modal-copy"
            }, [
              c(e.$slots, "default")
            ], 2)) : (o(), a("p", {
              key: 3,
              class: r([
                "d-modal__content",
                t.contentClass
              ]),
              "data-qa": "dt-modal-copy"
            }, h(t.copy), 3)),
            l.hasFooterSlot ? (o(), a("footer", Y, [
              c(e.$slots, "footer")
            ])) : g("", !0),
            t.hideClose ? (o(), m(S, {
              key: 5,
              onClose: l.close
            }, null, 8, ["onClose"])) : (o(), m(k, {
              key: 6,
              class: "d-modal__close",
              "data-qa": "dt-modal-close-button",
              size: "md",
              kind: "muted",
              importance: "clear",
              "aria-label": l.closeButtonTitle,
              title: l.closeButtonTitle,
              onClick: l.close
            }, {
              icon: u(({ iconSize: I }) => [
                E(C, { size: I }, null, 8, ["size"])
              ]),
              _: 1
            }, 8, ["aria-label", "title", "onClick"]))
          ], 10, W), [
            [q, t.show]
          ])
        ]),
        _: 3
      })
    ]),
    _: 3
  }, 16, ["show", "class", "aria-hidden"]);
}
const re = /* @__PURE__ */ j(U, [["render", G]]);
export {
  re as default
};
//# sourceMappingURL=modal.js.map
