import { createPopper as l } from "@popperjs/core";
import { C as i } from "./base-PQGakkrl.js";
import { C as a } from "./Element_Event_drag-DeZYlveu.js";
const r = ["click", "hover", "focus"], s = { fade: "fade", show: "show" }, p = {
  type: "tooltip",
  placement: "top",
  trigger: "click",
  theme: "white",
  wrap: "body",
  template: "",
  contentBody: "",
  contentHeader: "",
  selectorBody: "",
  selectorHead: "",
  selectorArrow: ".arrow",
  isLoad: !1,
  loadOnCallback: null,
  loadOffCallback: null,
  isRunTrigger: !0,
  isStopPropagation: !0,
  isInitShow: !0,
  isHtml: !0,
  className: ""
};
class u {
  elementModal;
  optionModal;
  pageData;
  pageObject;
  constructor(e, t) {
    this.elementModal = {}, this.elementModal.rely = e, this.optionModal = i.getJsonMerge(p, t), this.pageData = {
      isOpen: !1,
      events: []
    }, this.pageObject = null, ["listenerOn", "listenerOff", "listenerHide", "listenerRelatedMouseLeave"].forEach((o) => {
      this[o] = this[o].bind(this);
    }), this.init();
  }
  init() {
    if (!i.isEmpty(this.optionModal.template)) {
      const e = this.getEventType();
      this.setEventListener(e);
    }
  }
  getEventType() {
    const e = [], t = [];
    let o = [];
    return i.isArray(this.optionModal.trigger) ? o = this.optionModal.trigger : i.isString(this.optionModal.trigger) && (o = this.optionModal.trigger.split(",")), o.forEach((n) => {
      switch (i.isExistValue(r, n) ? n : "") {
        case "hover":
          e.push("mouseenter"), t.push("mouseleave");
          break;
        case "focus":
          e.push("focus"), t.push("blur");
          break;
        case "click":
          e.push("click"), t.push("click");
          break;
      }
    }), { on: e, off: t };
  }
  setEventListener(e) {
    i.isEmpty(e.on) || e.on.forEach((t) => {
      this.pageData.events.push({ on: t, func: this.listenerOn, eventName: t }), this.elementModal.rely.addEventListener(t, this.listenerOn);
    }), i.isEmpty(e.off) || e.off.forEach((t) => {
      this.pageData.events.push({ off: t, func: this.listenerOff, eventName: t }), this.elementModal.rely.addEventListener(t, this.listenerOff);
    });
  }
  listenerOn(e) {
    if (this.optionModal.isInitShow) {
      if (this.optionModal.isStopPropagation && (e.preventDefault(), e.stopPropagation()), this.pageData.isOpen || !this.optionModal.isRunTrigger)
        return;
      e.useOpenToTip = !0, this.show(), this.optionModal.isLoad && i.isFunction(this.optionModal.loadOnCallback) && this.optionModal.loadOnCallback();
    }
  }
  listenerOff(e) {
    !this.optionModal.isRunTrigger || i.isBoolean(e.useOpenToTip) && e.useOpenToTip || (this.hide(e), this.optionModal.isLoad && i.isFunction(this.optionModal.loadOffCallback) && this.optionModal.loadOffCallback());
  }
  show() {
    if (i.isString(this.optionModal.wrap)) {
      const e = window.document.querySelector(this.optionModal.wrap);
      if (i.isUndefined(e))
        return;
    }
    this.pageData.isOpen || this.operateShow();
  }
  operateShow() {
    if (!this.pageData.isOpen) {
      if (this.pageData.isOpen = !0, i.isUndefined(this.elementModal.tip)) {
        this.elementModal.tip = this.createElement();
        const t = this.getWrapElement();
        i.isUndefined(t) || t.appendChild(this.elementModal.tip);
        const o = {
          placement: this.optionModal.placement
        };
        o.modifiers = [
          {
            name: "offset",
            options: {
              offset: [0, 8]
            }
          }
        ], this.pageObject = l(this.elementModal.rely, this.elementModal.tip, o);
      } else
        this.pageObject.setOptions({
          modifiers: [{ name: "eventListeners", enabled: !0 }]
        });
      this.setContentBody(this.optionModal.contentBody), this.setContentHeader(this.optionModal.contentHeader);
      const e = [
        s.fade,
        s.show,
        `bs-${this.optionModal.type}-${this.optionModal.placement}`,
        `tooltip-${this.optionModal.theme}`
      ];
      i.isEmpty(this.optionModal.className) || this.optionModal.className.split(" ").forEach((t) => {
        i.isEmpty(t) || e.push(t);
      }), this.elementModal.tip.className = a.getAddClass(this.elementModal.tip.className, e), setTimeout(() => {
        this.pageObject.update();
      }, 50);
    }
  }
  createElement() {
    const e = window.document.createElement("div");
    return e.innerHTML = this.optionModal.template, e.childNodes[0];
  }
  setContentBody(e) {
    if (!i.isEmpty(this.optionModal.selectorBody) && !i.isEmpty(e))
      if (i.isUndefined(this.elementModal.tip))
        this.optionModal.contentBody = e;
      else {
        const t = this.elementModal.tip.querySelector(this.optionModal.selectorBody);
        if (!i.isUndefined(t)) {
          if (this.optionModal.isHtml) {
            const o = e.replace(/\\n/g, "<br>").replace(/\n/g, "<br>");
            t.innerHTML = o;
          } else
            t.innerText = e;
          this.optionModal.contentBody = e, this.pageObject.update();
        }
      }
  }
  setInitShow(e) {
    this.optionModal.isInitShow = e;
  }
  setContentHeader(e) {
    if (!i.isEmpty(this.optionModal.selectorHead)) {
      const t = this.elementModal.tip.querySelector(this.optionModal.selectorHead);
      if (!i.isUndefined(t)) {
        const o = i.isEmpty(e) ? "" : e;
        this.optionModal.isHtml ? t.innerHTML = o : t.innerText = o, this.optionModal.contentHeader = o, this.pageObject.update();
      }
    }
  }
  getWrapElement() {
    let e;
    return i.isString(this.optionModal.wrap) ? e = window.document.querySelector(this.optionModal.wrap) : this.optionModal.wrap === !1 && (e = this.elementModal.rely.parentNode), e;
  }
  hide(e) {
    if (!this.pageData.isOpen || !document.body.contains(this.elementModal.tip))
      return;
    let t = !0;
    if (e.type === "mouseleave") {
      const o = e.relatedreference || e.toElement || e.relatedTarget;
      this.elementModal.tip.contains(o) && (t = !1, this.elementModal.tip.addEventListener(e.type, this.listenerRelatedMouseLeave));
    }
    t && this.operateHide();
  }
  listenerRelatedMouseLeave(e) {
    const t = e.relatedreference || e.toElement || e.relatedTarget;
    this.elementModal.tip.removeEventListener(e.type, this.listenerRelatedMouseLeave), this.elementModal.rely.contains(t) || this.hide(e);
  }
  operateHide() {
    this.pageData.isOpen && (this.pageData.isOpen = !1, this.pageObject.setOptions({
      modifiers: [{ name: "eventListeners", enabled: !1 }]
    }), this.elementModal.tip.className = a.getRemoveClass(this.elementModal.tip.className, s.show), this.elementModal.tip.removeEventListener("mouseenter", this.listenerHide), this.elementModal.tip.removeEventListener("click", this.listenerHide), this.elementModal.tip.parentNode.removeChild(this.elementModal.tip), this.elementModal.tip = null);
  }
  listenerHide() {
    this.operateHide();
  }
  setRunTrigger(e) {
    this.optionModal.isRunTrigger = e;
  }
  dispose() {
    this.pageData.events.forEach(({ eventName: e, func: t }) => {
      this.elementModal.rely.removeEventListener(e, t);
    }), this.pageData.events = [], this.pageObject && (this.operateHide(), this.pageObject.destroy());
  }
  clShow() {
    this.operateShow();
  }
  clHide() {
    this.operateHide();
  }
  clToggle() {
    this.pageData.isOpen ? this.operateHide() : this.operateShow();
  }
  clSetContentBody(e) {
    this.setContentBody(e);
  }
  clSetContentHeader(e) {
    this.setContentHeader(e);
  }
  clSetRunTrigger(e) {
    this.setRunTrigger(e);
  }
  clSetInitShow(e) {
    this.setInitShow(e);
  }
  clDispose() {
    this.dispose();
  }
}
export {
  u as T
};
//# sourceMappingURL=tooltip-De0atcAJ.js.map
