import { getUniqueString as c } from "../../common/utils/index.js";
import n from "../tooltip/tooltip.js";
import { TOOLTIP_DIRECTIONS as u } from "../tooltip/tooltip-constants.js";
const T = {
  name: "dt-tooltip-directive",
  install(r) {
    const s = document.createElement("div");
    document.body.appendChild(s);
    const p = "top", a = new r({
      name: "DtTooltipDirectiveApp",
      components: { DtTooltip: n },
      data() {
        return {
          tooltips: []
        };
      },
      methods: {
        addOrUpdateTooltip(e, t) {
          const o = this.tooltips.findIndex((i) => i.id === e);
          o !== -1 ? this.tooltips.splice(o, 1, { id: e, ...t }) : this.tooltips.push({ id: e, ...t });
        },
        removeTooltip(e) {
          this.tooltips = this.tooltips.filter((t) => t.id !== e);
        }
      },
      render(e) {
        return e(
          "div",
          {
            domProps: { id: "dt-tooltip-directive-app" }
          },
          [
            this.tooltips.map(({ id: t, ...o }) => e(n, {
              key: t,
              props: {
                ...o,
                sticky: o.sticky !== void 0 ? o.sticky : !0,
                /**
                 * Set the delay to false when running tests only.
                 */
                delay: o.delay !== void 0 ? o.delay : process.env.NODE_ENV !== "test",
                externalAnchor: `[data-dt-tooltip-id="${t}"]`
              }
            }))
          ]
        );
      }
    });
    a.$mount(s), r.directive("dt-tooltip", {
      bind(e, t) {
        d(e, t);
      },
      update(e, t) {
        t.value !== t.oldValue && d(e, t);
      },
      unbind(e) {
        a.removeTooltip(e.getAttribute("data-dt-tooltip-id"));
      }
    });
    function d(e, t) {
      const o = e.getAttribute("data-dt-tooltip-id") || c();
      let i;
      if (typeof t.value == "string")
        i = {
          message: t.value,
          placement: t.arg || p
        };
      else if (typeof t.value == "object" && t.value !== null)
        i = {
          placement: t.arg || t.value.placement || p,
          ...t.value
        };
      else {
        console.error("DtTooltipDirective: binding value must be a string or object");
        return;
      }
      Object.keys(t.modifiers).forEach((l) => {
        switch (l) {
          case "inverted":
            i.inverted = !0;
            break;
          case "no-delay":
            i.delay = !1;
            break;
          case "no-transition":
            i.transition = !1;
            break;
          default:
            u.includes(l) && (i.placement = l);
            break;
        }
      }), e.setAttribute("data-dt-tooltip-id", o), a.addOrUpdateTooltip(o, i);
    }
  }
};
export {
  T as DtTooltipDirective,
  T as default
};
//# sourceMappingURL=tooltip.js.map
