import { defineComponent as Y, inject as Z, reactive as ee, ref as A, computed as $, watch as I, onMounted as te, nextTick as R, openBlock as p, createElementBlock as h, normalizeClass as _, createElementVNode as q, normalizeStyle as J, toDisplayString as M, unref as le, createCommentVNode as se, Fragment as ie, renderList as ae, renderSlot as ne } from "vue";
import { C as i } from "../Shared/base-PQGakkrl.js";
import { u as oe } from "../Shared/language-DxTlvFs2.js";
const ce = { class: "selectTag-container" }, ue = ["onClick"], re = ["innerHTML"], de = { key: 1 }, ve = /* @__PURE__ */ Y({
  name: "ClSelectTag",
  __name: "Index",
  props: {
    value: [String, Number],
    className: String,
    theme: {
      type: String,
      default: ""
      // default | interval
    },
    type: {
      type: String,
      required: !0,
      validator: (r) => ["checkbox", "radio"].indexOf(r) !== -1
    },
    list: {
      type: Array,
      default: () => []
    },
    disabled: {
      type: Boolean,
      default: !1
    },
    width: String,
    isShowAll: {
      type: Boolean,
      default: !1
    },
    showAllLabel: String,
    checkChangeBefore: Function,
    isInitRadioValue: {
      type: Boolean,
      default: !0
    },
    isRadioClickCancel: {
      type: Boolean,
      default: !1
    }
  },
  emits: ["update:value", "onChange", "onInit"],
  setup(r, { expose: j, emit: N }) {
    const a = r, E = N, { optPageLanguage: O, getPageLanguage: H } = oe();
    H("common");
    const C = Z("$ClGlobals"), x = ee({
      theme: ""
    }), K = `st-${i.getRandom(8)}`, V = A(!1), b = (e) => {
      let l = [];
      return i.isEmpty(e) || (l = e.toString().split(",")), l;
    }, o = A(b(a.value));
    let g = [], d = {};
    const u = A(!1), G = $(() => {
      const { className: e, theme: l } = a, t = {
        "panel-selectTag": !0
      };
      let s = x.theme;
      return i.isEmpty(l) || (s = l), i.isEmpty(s) || (t[`selectTag-${s}`] = !0), i.isEmpty(e) || e.split(" ").forEach((n) => {
        i.isEmpty(n) || (t[n] = !0);
      }), t;
    }), P = $(() => {
      const e = {};
      return i.isEmpty(a.width) || (e.width = a.width), e;
    }), z = (e) => {
      const l = {};
      return e.isHtml ? l.width = "auto" : i.isEmpty(e.width) ? i.isEmpty(a.width) || (l.width = a.width) : l.width = e.width, l;
    }, f = (e) => {
      const l = [];
      i.isEmpty(e) || e.forEach((c) => {
        i.isExistJsonKey(d, c) && l.push(i.getJsonClone(d[c]));
      });
      const t = e.join(","), s = l.length > 0 && l.length === g.length;
      let n;
      return a.type === "radio" ? (n = {}, l.length > 0 && (n = l[0])) : n = l, { value: t, info: n, isAll: s };
    }, k = (e) => {
      E("update:value", e);
    }, B = (e) => {
      if (!i.isEqualArr(o.value, e)) {
        const l = i.getArrClone(o.value), t = f(l);
        o.value = e;
        const { value: s, isAll: n, info: c } = f(e);
        k(s), E("onChange", s, c, n, {
          value: t.value,
          info: t.info
        });
      }
    }, L = () => {
      R(() => {
        const { value: e, isAll: l, info: t } = f(o.value);
        E("onInit", e, t, l);
      });
    }, Q = () => {
      if (C.globalsOption && C.globalsOption.selectTag) {
        const e = C.globalsOption.selectTag;
        i.isEmpty(e.theme) || (x.theme = e.theme);
      }
    }, S = (e) => {
      const l = [];
      if (e.length > 0) {
        const t = d[e[0]], s = t && !i.isEmpty(t.type) ? t.type : a.type;
        e.forEach((n) => {
          if (i.isExistJsonKey(d, n)) {
            const c = d[n];
            if ((i.isEmpty(c.type) ? a.type : c.type) === s)
              switch (s) {
                case "radio":
                  l.length === 0 && l.push(n);
                  break;
                case "checkbox":
                  l.push(n);
                  break;
              }
          }
        });
      }
      return l;
    }, T = (e) => {
      if (d = {}, g = [], !i.isEmpty(e)) {
        const l = b(a.value);
        e.forEach((t) => {
          if (i.isExistJsonKey(t, "id")) {
            const s = t.id.toString();
            d[s] = t, t.isDisabled ? i.isExistValue(l, s) && g.push(s) : g.push(s);
          }
        });
      }
    }, m = () => {
      if (u.value) {
        let e = [];
        i.isEmpty(o.value) || (e = S(o.value), i.isEqualArr(o.value, e) || (o.value = e, k(e.join(",")))), a.isShowAll && (V.value = e.length > 0 && e.length === g.length);
      }
    }, v = () => {
      if (a.type === "radio" && a.isInitRadioValue && i.isEmpty(o.value) && a.list.length > 0) {
        const e = a.list[0].id.toString();
        o.value = [e], k(e);
      }
    }, w = (e) => {
      const l = b(e);
      let t = i.isEqualArr(o.value, l);
      t && (t = e === o.value.join(",")), t || (o.value = l);
    }, U = () => {
      Q(), u.value = a.list.length !== 0, T(a.list), v(), m();
    };
    I(u, (e) => {
      e && L();
    }), I(
      () => a.list,
      (e) => {
        u.value || (u.value = !0, e.length === 0 && m());
      }
    ), I(
      [() => a.list, () => a.value],
      ([e, l], [t, s]) => {
        const n = e;
        !u.value && n.length > 0 && (u.value = !0), T(n), l !== s && w(l), v(), m();
      }
    ), te(() => {
      R(() => {
        U();
      });
    });
    const W = () => {
      if (!a.disabled) {
        const e = [];
        a.list.forEach((t) => {
          const s = t.id.toString();
          t.isDisabled ? i.isExistValue(o.value, s) && e.push(s) : V.value || e.push(s);
        });
        const l = S(e);
        B(l);
      }
    }, X = async (e) => {
      if (!e.isDisabled && !a.disabled) {
        let l = a.type;
        l = i.isEmpty(e.type) ? l : e.type;
        const t = i.getArrClone(o.value);
        let s = [];
        const n = e.id.toString();
        if (t.length === 0)
          s = [n];
        else if (l === "radio")
          i.isEqualArr(t, [n]) ? s = a.isRadioClickCancel ? [] : t : s = [n];
        else {
          const y = d[t[0]];
          (i.isEmpty(y.type) ? a.type : y.type) === "radio" ? s = [n] : i.isExistValue(t, n) ? s = i.getArrRemoveById(t, n) : (s = t, s.push(n));
        }
        let c = !0;
        if (a.checkChangeBefore) {
          const { value: y } = f(s), F = f(t);
          c = await a.checkChangeBefore(y, F.value), c = i.isBoolean(c) ? c : !0;
        }
        c && B(s);
      }
    }, D = () => S(o.value).join(",");
    return j({
      getValue: D,
      getValueData: () => {
        const e = D(), { info: l } = f(e.split(","));
        return l;
      },
      setValue: (e) => {
        w(e), v(), m();
      },
      runInitComplete: () => {
        u.value || (u.value = !0, T(a.list), w(a.value), v(), m(), L());
      }
    }), (e, l) => (p(), h("div", {
      class: _(G.value)
    }, [
      q("ul", ce, [
        r.isShowAll ? (p(), h("li", {
          key: 0,
          class: _({
            "selectTag-item": !0,
            "selectTag-active": V.value,
            "selectTag-disabled": r.disabled
          }),
          style: J(P.value),
          onClick: l[0] || (l[0] = (t) => W())
        }, [
          q("span", null, M(r.showAllLabel || le(O).checkboxAll), 1)
        ], 6)) : se("", !0),
        (p(!0), h(ie, null, ae(r.list, (t, s) => (p(), h("li", {
          key: `${K}-${s}`,
          class: _({
            "selectTag-item": !0,
            "selectTag-active": o.value.includes(t.id.toString()),
            "selectTag-disabled": t.isDisabled || r.disabled
          }),
          style: J(z(t)),
          onClick: (n) => X(t)
        }, [
          ne(e.$slots, "default", { data: t }, () => [
            t.isHtml ? (p(), h("span", {
              key: 0,
              innerHTML: t.name
            }, null, 8, re)) : (p(), h("span", de, M(t.name), 1))
          ])
        ], 14, ue))), 128))
      ])
    ], 2));
  }
});
export {
  ve as default
};
//# sourceMappingURL=ClSelectTag.js.map
