import { defineComponent as b, mergeModels as N, useModel as T, ref as m, computed as V, watch as p, onBeforeMount as E, createBlock as h, openBlock as S, unref as B } from "vue";
import { u as D, h as A } from "./start-BYfL32Oe.mjs";
import "vue-i18n";
import "pinia";
import "vue-router";
import "./header-Droxy2Kd.mjs";
import "./moment-h96o7c8I.mjs";
import { A as M, S as I } from "./constants-CwfsvyFM.mjs";
import { c as L, a as _ } from "./utils-PF9w8IEt.mjs";
import { u as k } from "./dataTypes-nQ5Ht0CH.mjs";
const z = /* @__PURE__ */ b({
  __name: "DatahubDataTypeTreeNodeValueEditor",
  props: /* @__PURE__ */ N({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(f) {
    const l = f, r = T(f, "modelValue"), { root: v, dispatchEvent: c } = D(), y = k(), { fetchTreeNodes: g } = y, i = m([]), u = m(!1), n = m([]), s = V(() => {
      var t;
      const { multiSelect: e, flat: a } = ((t = l.attribute) == null ? void 0 : t.configuration) ?? {};
      return !e && a;
    });
    return p(
      () => {
        var e;
        return (e = l.attribute) == null ? void 0 : e.id;
      },
      async (e) => {
        if (e)
          try {
            u.value = !0;
            const a = await g(
              {
                // entityId: this.entity ? this.entity.id : null,
                attributeId: e,
                scopeId: 0,
                languageId: 0
              },
              l.attribute
            );
            if (!a) {
              n.value = [];
              return;
            }
            const {
              data: {
                tree: { children: t }
              }
            } = a, o = [];
            L(o, t, 0), n.value = o, s.value && c({
              event: M,
              data: {
                items: n.value,
                attribute: l.attribute,
                fieldName: l.fieldName
              }
            });
          } catch (a) {
            console.log(a.message);
          } finally {
            u.value = !1;
          }
      },
      { immediate: !0 }
    ), p(
      [i, u],
      ([e, a]) => {
        if (a)
          return;
        const t = Array.isArray(e) ? e.map((o) => ({ id: o })) : [{ id: e }];
        r.value && t.length === r.value.length && r.value.every(({ id: o }) => t.find((d) => d.id === o)) || (r.value = t);
      },
      { deep: !0 }
    ), E(() => {
      var a;
      const e = ((a = r.value) == null ? void 0 : a.map(({ id: t }) => Number(t))) ?? [];
      s.value ? i.value = e[0] : i.value = e;
    }), p(
      r,
      (e) => {
        e && c({
          event: I,
          data: {
            asString: _(e),
            asValue: e.map(
              ({ id: a }) => n.value.find((t) => t.id === a)
            ),
            attribute: l.attribute,
            fieldName: l.fieldName
          }
        });
      },
      { deep: !0 }
    ), (e, a) => {
      var t, o;
      return S(), h(B(A), {
        ref_key: "root",
        ref: v,
        modelValue: i.value,
        "onUpdate:modelValue": a[0] || (a[0] = (d) => i.value = d),
        items: n.value,
        readonly: ((o = (t = e.attribute) == null ? void 0 : t.configuration) == null ? void 0 : o.readOnly) || e.readOnly,
        label: e.massEditing ? void 0 : e.label,
        loading: u.value || e.loading,
        multiple: !s.value,
        "item-title": "label",
        "item-value": "id",
        "persistent-placeholder": ""
      }, null, 8, ["modelValue", "items", "readonly", "label", "loading", "multiple"]);
    };
  }
});
export {
  z as default
};
