import { defineComponent as b, reactive as j, ref as z, computed as C, onMounted as A, nextTick as q, watch as L, resolveComponent as D, openBlock as v, createElementBlock as I, normalizeClass as W, Fragment as H, renderList as Q, normalizeStyle as X, createVNode as w, withCtx as Y } from "vue";
import { C as a } from "../Shared/base-PQGakkrl.js";
import { u as Z } from "../Shared/language-DxTlvFs2.js";
const oe = /* @__PURE__ */ b({
  name: "ClSelectCascade",
  __name: "Index",
  props: {
    value: [String, Number],
    list: {
      type: Array,
      default: () => []
    },
    placeholder: String,
    className: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: !1
    },
    isClear: {
      type: Boolean,
      default: !1
    },
    isSearch: {
      type: Boolean,
      default: !1
    },
    parentKey: {
      type: String,
      default: "parentId"
    },
    topParentValue: {
      type: String,
      default: "0"
    },
    width: String,
    isCheckValidate: {
      type: Boolean,
      default: !1
    },
    validateRules: {
      type: Array,
      default: () => [{ r: "required" }]
    },
    isShowInMsg: {
      type: Boolean,
      default: !0
    }
  },
  emits: ["update:value", "onChange"],
  setup(d, { expose: B, emit: F }) {
    const r = d, E = F, { optPageLanguage: K, getPageLanguage: M } = Z();
    M("common");
    const u = j({
      keyIndex: `selectCascade-${a.getRandom(8)}`,
      lastValue: r.value,
      isFirstDataFinish: !1
    }), m = z([]), c = {
      dataJson: {},
      dataGroup: {},
      parentIdsJson: {},
      defPlaceholder: K.value.pleaseSelect
    }, U = C(() => {
      const { placeholder: e } = r;
      return e && !a.isEmpty(e) ? e.split("|") : [];
    }), x = C(() => {
      const { width: e } = r;
      return e && !a.isEmpty(e) ? e.split("|") : [];
    }), G = C(() => {
      const e = {
        "panel-selectCascade": !0
      }, { className: t } = r;
      return t && !a.isEmpty(t) && t.split(" ").forEach((s) => {
        a.isEmpty(s) || (e[s] = !0);
      }), e;
    }), N = (e) => {
      e !== u.lastValue && (u.lastValue = e, E("update:value", e), E("onChange", e));
    }, V = (e) => a.isExistJsonKey(c.dataGroup, e.toString()), O = (e) => {
      const t = e.toString();
      return V(t) ? c.dataGroup[t] : [];
    }, J = (e) => {
      const t = e.toString();
      return a.isExistJsonKey(c.dataJson, t) ? c.dataJson[t] : null;
    }, y = () => `top_${r.topParentValue}`, P = (e) => {
      const { parentKey: t, topParentValue: s } = r, n = {}, o = {};
      a.isEmpty(e) || e.forEach((i) => {
        const l = a.getJsonClone(i), S = l.id.toString(), h = l[t].toString();
        n[S] = l;
        const g = h === s ? y() : h;
        a.isExistJsonKey(o, g) || (o[g] = []), o[g].push(l);
      }), c.dataJson = n, c.dataGroup = o;
    }, p = (e, t) => {
      const s = {
        idx: e,
        pId: t,
        placeholder: c.defPlaceholder,
        list: [],
        sty: {},
        selected: ""
      };
      if (!a.isEmpty(t)) {
        const l = O(t);
        s.list = a.getListClone(l);
      }
      const n = U.value, o = n.length;
      o > 0 && (o > e ? s.placeholder = a.isEmpty(n[e]) ? c.defPlaceholder : n[e] : o === 1 && (s.placeholder = n[0]));
      const i = x.value;
      return i.length > e && (a.isEmpty(i[e]) || (s.sty.width = i[e], s.sty.flex = "none")), s;
    }, _ = (e) => {
      const { parentKey: t, topParentValue: s } = r;
      let n = [];
      if (a.isExistJsonKey(c.parentIdsJson, e) && c.parentIdsJson[e].length > 0)
        n = c.parentIdsJson[e];
      else {
        const o = J(e);
        if (!a.isUndefined(o)) {
          const i = o[t].toString();
          if (i !== s && !a.isUndefined(J(i))) {
            n.push(i);
            const l = _(i);
            l.length > 0 && (n = n.concat(l));
          }
        }
        c.parentIdsJson[e] = n;
      }
      return n;
    }, f = (e) => {
      const t = a.isUndefined(e) ? r.value : e, s = [];
      if (a.isEmpty(t))
        s.push(p(0, y())), s.push(p(1, ""));
      else {
        const n = t.toString(), o = _(n), i = o.length;
        if (i > 0) {
          o.forEach((S, h) => {
            const g = h === 0 ? y() : S, k = p(h, g);
            k.selected = S, s.push(k);
          });
          const l = p(i, o[i - 1]);
          l.selected = n, s.push(l);
        } else {
          const l = p(0, y());
          l.selected = n, s.push(l);
        }
      }
      m.value = s;
    }, R = () => {
      P(r.list), f();
    };
    A(() => {
      q(() => {
        R();
      });
    }), L(
      () => r.value,
      (e, t) => {
        e !== t && !a.isUndefined(e) && e !== u.lastValue && u.isFirstDataFinish && (f(), u.lastValue = e);
      }
    ), L(
      () => r.list,
      (e) => {
        u.isFirstDataFinish || (u.isFirstDataFinish = !0), c.parentIdsJson = {}, P(e);
        const { value: t } = r, s = t && !a.isEmpty(t) && a.isExistJsonKey(c.dataJson, t.toString()) ? t : "";
        f(s), u.lastValue = s;
      }
    );
    const $ = (e) => (console.log(e), !0), T = (e, t) => {
      let s = "";
      if (a.isEmpty(e) && t === 0)
        f("");
      else {
        const n = e.toString();
        V(n) ? (m.value.splice(t + 1), m.value.push(p(t + 1, n))) : s = e;
      }
      N(s);
    };
    return B({
      getValue: () => {
      },
      setValue: (e) => {
        f(e);
      }
    }), (e, t) => {
      const s = D("cl-select"), n = D("cl-form-validate");
      return v(), I("div", {
        class: W(G.value)
      }, [
        (v(!0), I(H, null, Q(m.value, (o, i) => (v(), I("div", {
          key: `${u.keyIndex}-${i}`,
          class: "selectCascade-item",
          style: X(o.sty)
        }, [
          w(n, {
            isShowMsg: d.isShowInMsg,
            isCheck: d.isCheckValidate,
            checkValue: o.selected,
            rules: d.validateRules
          }, {
            default: Y(() => [
              w(s, {
                isClear: d.isClear,
                isSearch: d.isSearch,
                placeholder: o.placeholder,
                list: o.list,
                value: o.selected,
                "onUpdate:value": (l) => o.selected = l,
                onOpenBefore: $,
                onOnChange: (l) => T(l, i)
              }, null, 8, ["isClear", "isSearch", "placeholder", "list", "value", "onUpdate:value", "onOnChange"])
            ]),
            _: 2
          }, 1032, ["isShowMsg", "isCheck", "checkValue", "rules"])
        ], 4))), 128))
      ], 2);
    };
  }
});
export {
  oe as default
};
//# sourceMappingURL=ClSelectCascade.js.map
