import { C as i } from "../../Shared/base-PQGakkrl.js";
import { createPopper as u } from "@popperjs/core";
import { C as c } from "../../Shared/index-BnerInzC.js";
const f = ["click", "hover", "focus"], h = { fade: "fade", show: "show" }, m = {
  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 g {
  elementModal;
  optionModal;
  pageData;
  pageObject;
  constructor(e, o) {
    this.elementModal = {}, this.elementModal.rely = e, this.optionModal = i.getJsonMerge(m, o), this.pageData = {
      isOpen: !1,
      events: []
    }, this.pageObject = null, ["listenerOn", "listenerOff", "listenerHide", "listenerRelatedMouseLeave"].forEach((l) => {
      this[l] = this[l].bind(this);
    }), this.init();
  }
  init() {
    if (!i.isEmpty(this.optionModal.template)) {
      const e = this.getEventType();
      this.setEventListener(e);
    }
  }
  getEventType() {
    const e = [], o = [];
    let l = [];
    return i.isArray(this.optionModal.trigger) ? l = this.optionModal.trigger : i.isString(this.optionModal.trigger) && (l = this.optionModal.trigger.split(",")), l.forEach((a) => {
      switch (i.isExistValue(f, a) ? a : "") {
        case "hover":
          e.push("mouseenter"), o.push("mouseleave");
          break;
        case "focus":
          e.push("focus"), o.push("blur");
          break;
        case "click":
          e.push("click"), o.push("click");
          break;
      }
    }), { on: e, off: o };
  }
  setEventListener(e) {
    i.isEmpty(e.on) || e.on.forEach((o) => {
      this.pageData.events.push({ on: o, func: this.listenerOn, eventName: o }), this.elementModal.rely.addEventListener(o, this.listenerOn);
    }), i.isEmpty(e.off) || e.off.forEach((o) => {
      this.pageData.events.push({ off: o, func: this.listenerOff, eventName: o }), this.elementModal.rely.addEventListener(o, 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 o = this.getWrapElement();
        i.isUndefined(o) || o.appendChild(this.elementModal.tip);
        const l = {
          placement: this.optionModal.placement
        };
        l.modifiers = [
          {
            name: "offset",
            options: {
              offset: [0, 8]
            }
          }
        ], this.pageObject = u(this.elementModal.rely, this.elementModal.tip, l);
      } else
        this.pageObject.setOptions({
          modifiers: [{ name: "eventListeners", enabled: !0 }]
        });
      this.setContentBody(this.optionModal.contentBody), this.setContentHeader(this.optionModal.contentHeader);
      const e = [
        h.fade,
        h.show,
        `bs-${this.optionModal.type}-${this.optionModal.placement}`,
        `tooltip-${this.optionModal.theme}`
      ];
      i.isEmpty(this.optionModal.className) || this.optionModal.className.split(" ").forEach((o) => {
        i.isEmpty(o) || e.push(o);
      }), this.elementModal.tip.className = c.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 o = this.elementModal.tip.querySelector(this.optionModal.selectorBody);
        if (!i.isUndefined(o)) {
          if (this.optionModal.isHtml) {
            const l = e.replace(/\\n/g, "<br>").replace(/\n/g, "<br>");
            o.innerHTML = l;
          } else
            o.innerText = e;
          this.optionModal.contentBody = e, this.pageObject.update();
        }
      }
  }
  setInitShow(e) {
    this.optionModal.isInitShow = e;
  }
  setContentHeader(e) {
    if (!i.isEmpty(this.optionModal.selectorHead)) {
      const o = this.elementModal.tip.querySelector(this.optionModal.selectorHead);
      if (!i.isUndefined(o)) {
        const l = i.isEmpty(e) ? "" : e;
        this.optionModal.isHtml ? o.innerHTML = l : o.innerText = l, this.optionModal.contentHeader = l, 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 o = !0;
    if (e.type === "mouseleave") {
      const l = e.relatedreference || e.toElement || e.relatedTarget;
      this.elementModal.tip.contains(l) && (o = !1, this.elementModal.tip.addEventListener(e.type, this.listenerRelatedMouseLeave));
    }
    o && this.operateHide();
  }
  listenerRelatedMouseLeave(e) {
    const o = e.relatedreference || e.toElement || e.relatedTarget;
    this.elementModal.tip.removeEventListener(e.type, this.listenerRelatedMouseLeave), this.elementModal.rely.contains(o) || this.hide(e);
  }
  operateHide() {
    this.pageData.isOpen && (this.pageData.isOpen = !1, this.pageObject.setOptions({
      modifiers: [{ name: "eventListeners", enabled: !1 }]
    }), this.elementModal.tip.className = c.getRemoveClass(this.elementModal.tip.className, h.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: o }) => {
      this.elementModal.rely.removeEventListener(e, o);
    }), 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();
  }
}
const s = {
  positions: {
    top: "top",
    right: "right",
    rightTop: "right-start",
    bottom: "bottom",
    left: "left",
    leftTop: "left-start"
  },
  triggers: ["hover", "click", "focus"],
  themes: ["white", "black"],
  options: {
    position: "top",
    // top | right | bottom | left | rightTop
    trigger: "hover",
    // hover | focus | click
    theme: "white",
    // white | black
    template: "<div class='tooltip'><div class='tooltip-inner'></div><div class='arrow' data-popper-arrow></div></div>",
    selectorBody: ".tooltip-inner",
    selectorArrow: ".arrow",
    isHtml: !0,
    isRunTrigger: !0,
    isFixedVisible: !1,
    // todo 待定
    isStopPropagation: !0,
    className: ""
  },
  bind: (t, e) => {
    s.create(t, e);
  },
  create: (t, e) => {
    if (i.isObject(e) && !i.isUndefined(e.value) && s.getIsInit(e)) {
      const l = s._getValueHtml(e.value);
      if (!i.isEmpty(l)) {
        const a = s.getOptions();
        a.contentBody = l, a.placement = s.getPosition(e), a.trigger = s.getTrigger(e), a.theme = s.getTheme(e), a.isHtml = s.getIsHtml(e), a.isFixedVisible = s.getFixedVisible(e), a.isStopPropagation = s.getStopPropagation(e), a.className = s.getClassName(e);
        const n = new g(t, a);
        n._vueEl = t, t._tooltip = n;
      }
    }
  },
  getIsInit: (t) => i.isObject(t.value) && i.isBoolean(t.value.init) ? t.value.init : !0,
  getOptions: () => i.getJsonClone(s.options),
  _getValueHtml: (t) => {
    let e;
    switch (typeof t) {
      case "string":
      case "number":
        e = t.toString();
        break;
      case "object":
        i.isEmpty(t.html) || (e = t.html);
        break;
      default:
        e = "";
    }
    return e;
  },
  getPosition: (t) => {
    let e = i.isObject(t.value) ? t.value.position : "";
    if (i.isEmpty(e))
      for (const [o, l] of Object.entries(s.positions))
        t.modifiers[o] && (e = l);
    else
      i.isExistJsonKey(s.positions, e) && (e = s.positions[e]);
    return i.isEmpty(e) ? s.options.position : e;
  },
  getTrigger: (t) => {
    let e = i.isObject(t.value) ? t.value.trigger : "";
    return i.isEmpty(e) && s.triggers.forEach((o) => {
      t.modifiers[o] && (e = o);
    }), i.isEmpty(e) ? s.options.trigger : e;
  },
  getTheme: (t) => {
    let e = i.isObject(t.value) ? t.value.theme : "";
    return i.isEmpty(e) && s.themes.forEach((o) => {
      t.modifiers[o] && (e = o);
    }), i.isEmpty(e) ? s.options.theme : e;
  },
  getIsHtml: (t) => {
    const e = i.isObject(t.value) ? t.value.isHtml : !0;
    return i.isBoolean(e) ? e : s.options.isFixedVisible;
  },
  getIsRunTrigger: (t) => i.isObject(t) && i.isBoolean(t.isRunTrigger) ? t.isRunTrigger : !0,
  getFixedVisible: (t) => {
    const e = i.isObject(t.value) ? t.value.isFixedVisible : !1;
    return i.isBoolean(e) ? e : s.options.isFixedVisible;
  },
  getStopPropagation: (t) => {
    const e = i.isObject(t.value) ? t.value.isStopPropagation : !1;
    return i.isBoolean(e) ? e : s.options.isStopPropagation;
  },
  getClassName: (t) => {
    const e = i.isObject(t.value) ? t.value.className : "";
    return i.isEmpty(e) ? s.options.className : e;
  },
  update: (t, e) => {
    const o = s.getIsInit(e);
    if (o) {
      !t._tooltip && o && s.create(t, e);
      const l = s._getValueHtml(e.value), a = s._getValueHtml(e.oldValue), n = i.isObject(e.value);
      let p = !1;
      if (l !== a)
        p = !0;
      else if (n && !i.isUndefined(e.value.updRefreshInx)) {
        const r = e.value.updRefreshInx, d = e.oldValue.updRefreshInx;
        r !== d && (p = !0);
      }
      if (t._tooltip) {
        p && t._tooltip.clSetContentBody(l), t._tooltip.clSetInitShow(o);
        const r = s.getIsRunTrigger(e.value), d = s.getIsRunTrigger(e.oldValue);
        (!r || !o) && t._tooltip.clHide(), r !== d && t._tooltip.clSetRunTrigger(r);
      }
    } else
      s.destroy(t);
  },
  destroy: (t) => {
    t._tooltip && (t._tooltip.clDispose(), delete t._tooltip);
  },
  show: (t) => {
    t._tooltip && t._tooltip.clShow();
  },
  hide: (t) => {
    t._tooltip && t._tooltip.clHide();
  }
}, O = "clTooltip", w = () => ({
  deep: !0,
  mounted: s.bind,
  updated: s.update,
  unmounted: s.destroy
}), E = {
  mounted: s.bind,
  updated: s.update
}, T = {
  show: s.show,
  hide: s.hide
};
export {
  w as directive,
  O as name,
  T as tooltipCommon,
  E as tooltipDirectives
};
//# sourceMappingURL=Tooltip.js.map
