import { defineComponent as V, mergeModels as c, useModel as E, ref as d, computed as h, watch as m, nextTick as B, onBeforeMount as S, createBlock as D, openBlock as A, unref as M } from "vue";
import { u as k, a as I } from "./start-CE9SawgU.js";
import "vue-i18n";
import "pinia";
import "./header-Bn1EKnBE.js";
import { A as L, S as _ } from "./constants-CwfsvyFM.js";
import { c as w, a as O } from "./utils-BfOsse_-.js";
import { u as z } from "./dataTypes-DocW2t0g.js";
const H = /* @__PURE__ */ V({
  __name: "DatahubDataTypeTreeNodeValueEditor",
  props: /* @__PURE__ */ c({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    succeeded: { type: Boolean }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ c(["validated", "update:model-value", "initialized"], ["update:modelValue"]),
  setup(p, { emit: v }) {
    const l = p, y = v, o = E(p, "modelValue"), { root: g, dispatchEvent: f } = k(), b = z(), { fetchTreeNodes: N } = b, r = d([]), u = d(!1), n = d([]), s = h(() => {
      const { multiSelect: e, flat: a } = l.attribute?.configuration ?? {};
      return !e && a;
    });
    return m(
      () => l.attribute?.id,
      async (e) => {
        if (e)
          try {
            u.value = !0;
            const a = await N(
              {
                // 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, i = [];
            w(i, t, 0), n.value = i, s.value && f({
              event: L,
              data: {
                items: n.value,
                attribute: l.attribute,
                fieldName: l.fieldName
              }
            });
          } catch (a) {
            console.log(a.message);
          } finally {
            u.value = !1, await B(), y("initialized");
          }
      },
      { immediate: !0 }
    ), m(
      [r, u],
      ([e, a]) => {
        if (a)
          return;
        const t = Array.isArray(e) ? e.map((i) => ({ id: i })) : [{ id: e }];
        o.value && t.length === o.value.length && o.value.every(({ id: i }) => t.find((T) => T.id === i)) || (o.value = t);
      },
      { deep: !0 }
    ), S(() => {
      const e = o.value?.map(({ id: a }) => Number(a)) ?? [];
      s.value ? r.value = e[0] : r.value = e;
    }), m(
      o,
      (e) => {
        e && f({
          event: _,
          data: {
            asString: O(e),
            asValue: e.map(
              ({ id: a }) => n.value.find((t) => t.id === a)
            ),
            attribute: l.attribute,
            fieldName: l.fieldName
          }
        });
      },
      { deep: !0 }
    ), (e, a) => (A(), D(M(I), {
      ref_key: "root",
      ref: g,
      modelValue: r.value,
      "onUpdate:modelValue": a[0] || (a[0] = (t) => r.value = t),
      items: n.value,
      readonly: e.attribute?.configuration?.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 {
  H as default
};
