import { defineComponent as U, mergeModels as C, useModel as H, computed as P, ref as V, watch as W, createBlock as I, openBlock as E, unref as e, withCtx as v, createCommentVNode as Q, withModifiers as de, createVNode as A, mergeProps as j, createElementBlock as re, Fragment as me, isRef as F } from "vue";
import { useI18n as L } from "vue-i18n";
import { storeToRefs as R } from "pinia";
import { o as $, I as z, ad as pe, f as ce, g as be, a as O, h as J, E as K, i as ve, c as Ae, aQ as fe, W as ge, aa as he, a3 as ye, al as Se, aR as G } from "./start-BYfL32Oe.mjs";
import "vue-router";
import "./header-Droxy2Kd.mjs";
import "./moment-h96o7c8I.mjs";
import { B as Ee } from "./base-CZBAM3Up.mjs";
import { e as q } from "./ETLAttributeMaps-CQdXyXVn.mjs";
import { c as k, h as Me, T as N } from "./topBarRoutes-0VuQ_i6D.mjs";
import { E as Te } from "./EditAttributeMapping-DgbKIxwV.mjs";
import { a as Ie, b as Ve } from "./tables-C7wXlu25.mjs";
const D = `${Ee}CalculatedEntityFields.`, B = {
  /** Fetches the ETL attribute set map and ETL attribute map to allow creation/editing of the mapping of a calculated entity field. */
  async getMaps(r) {
    return $.post(`${D}getMaps`, r);
  },
  /** deletes calculated mappings, the source attribute set is equal to the destination attribute */
  async deleteCalculatedAttributeMap(r, n) {
    return $.post(`${D}deleteCalculatedAttributeMap`, {
      attributeSetId: r,
      destinationAttributeId: n
    });
  },
  /** update older entities when a new automatic mapping is created. */
  async recalculateEntityFields(r) {
    return $.post(`${D}recalculateEntityFields`, r);
  }
}, we = /* @__PURE__ */ U({
  __name: "AssignedSetDetails",
  props: /* @__PURE__ */ C({
    attributeMap: {},
    pluginName: {}
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ C(["save"], ["update:modelValue"]),
  setup(r) {
    const n = r, c = H(r, "modelValue"), { t: M } = L(), { isLoadingDataTypes: w, dataTypes: S } = R(
      k(n.pluginName)
    ), T = P(() => {
      var d, f;
      const l = ((d = n.attributeMap) == null ? void 0 : d.dhAttribute.name) ?? "", i = ((f = n.attributeMap) == null ? void 0 : f.attributeSetName) ?? "";
      return M("datahub-ui.dh-shared.editAssignedAttribute", {
        attribute: l,
        attributeSet: i
      });
    });
    T.value;
    const s = V();
    return W(
      () => n.attributeMap,
      () => {
        var l, i, d, f;
        s.value = ((l = n.attributeMap) == null ? void 0 : l.configuration) ?? ((f = (d = (i = n.attributeMap) == null ? void 0 : i.dhAttribute) == null ? void 0 : d.dataTypeInstance) == null ? void 0 : f.defaultConfiguration);
      },
      { immediate: !0 }
    ), (l, i) => (E(), I(e(z), {
      modelValue: c.value,
      "onUpdate:modelValue": i[3] || (i[3] = (d) => c.value = d),
      title: T.value,
      "min-width": "600px",
      "max-width": "800px"
    }, {
      actions: v(() => [
        A(e(K), {
          variant: "elevated",
          text: l.$t("common.applyChanges"),
          onClick: i[0] || (i[0] = (d) => l.$emit("save", s.value))
        }, null, 8, ["text"])
      ]),
      default: v(() => [
        l.attributeMap ? (E(), I(e(pe), {
          key: 0,
          class: "h-100 overflow-y-auto overflow-x-hidden",
          onSubmit: i[2] || (i[2] = de(() => {
          }, ["prevent"]))
        }, {
          default: v(() => [
            A(e(ce), {
              class: "pa-2 mt-1",
              fluid: ""
            }, {
              default: v(() => [
                A(e(be), null, {
                  default: v(() => [
                    A(e(O), null, {
                      default: v(() => [
                        A(e(J), {
                          "model-value": l.attributeMap.dhAttribute.dataType,
                          label: l.$t("datahub-ui.dh-table.dataType"),
                          loading: e(w),
                          items: e(S),
                          "item-title": "name",
                          "item-value": "id",
                          readonly: ""
                        }, null, 8, ["model-value", "label", "loading", "items"])
                      ]),
                      _: 1
                    }),
                    A(e(O), null, {
                      default: v(() => [
                        A(Me, {
                          modelValue: s.value,
                          "onUpdate:modelValue": i[1] || (i[1] = (d) => s.value = d),
                          attribute: l.attributeMap.dhAttribute,
                          "data-type-instance": l.attributeMap.dhAttribute.dataTypeInstance,
                          "remote-scopes": [],
                          "remote-languages": []
                        }, null, 8, ["modelValue", "attribute", "data-type-instance"])
                      ]),
                      _: 1
                    })
                  ]),
                  _: 1
                })
              ]),
              _: 1
            })
          ]),
          _: 1
        })) : Q("", !0)
      ]),
      _: 1
    }, 8, ["modelValue", "title"]));
  }
}), Ne = /* @__PURE__ */ U({
  __name: "AddAssignedAttributes",
  props: /* @__PURE__ */ C({
    pluginName: {}
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ C(["save"], ["update:modelValue"]),
  setup(r) {
    const n = r, c = H(r, "modelValue"), { t: M } = L(), w = k(n.pluginName), { availableAttributes: S, isLoadingAttributes: T } = R(
      w
    ), s = V([]), l = P(
      () => s.value.length > 0 && s.value.every(({ attributeId: m }) => m)
    );
    function i() {
      s.value.push({});
    }
    function d(m, h) {
      s.value = s.value.map(
        (p, b) => b === h ? { ...p, ...m } : p
      );
    }
    function f({ rowIndex: m }) {
      s.value.splice(m, 1);
    }
    return W(c, (m) => {
      m && (s.value = []);
    }), (m, h) => (E(), I(e(z), {
      modelValue: c.value,
      "onUpdate:modelValue": h[1] || (h[1] = (p) => c.value = p),
      title: e(M)("datahub-ui.dh-shared.newAttributeAssignment"),
      "min-width": "600px",
      "max-width": "800px"
    }, {
      actions: v(() => [
        A(e(K), {
          variant: "elevated",
          disabled: !l.value,
          text: e(M)("datahub-ui.dh-shared.applyAttributeAssignments"),
          onClick: h[0] || (h[0] = (p) => m.$emit("save", s.value))
        }, null, 8, ["disabled", "text"])
      ]),
      default: v(() => [
        A(e(ve), j({
          data: s.value,
          "create-actions": [
            {
              title: m.$t("datahub-ui.dh-shared.newAttributeSet"),
              icon: "add_circle"
            }
          ]
        }, e(Ie), {
          onCreate: i,
          onRemove: f
        }), {
          "cell-attributeId": v(({ rowIndex: p, cellValue: b }) => [
            A(e(J), {
              "model-value": b,
              items: e(S),
              loading: e(T),
              "item-title": "name",
              "item-value": "id",
              search: "",
              "page-size": "",
              "onUpdate:modelValue": (y) => d({ attributeId: y }, p)
            }, null, 8, ["model-value", "items", "loading", "onUpdate:modelValue"])
          ]),
          "cell-requireFlag": v(({ rowIndex: p, cellValue: b }) => [
            A(e(Ae), {
              "model-value": b,
              label: m.$t("datahub-ui.dh-shared.required"),
              "onUpdate:modelValue": (y) => d({ requireFlag: y }, p)
            }, null, 8, ["model-value", "label", "onUpdate:modelValue"])
          ]),
          _: 1
        }, 16, ["data", "create-actions"])
      ]),
      _: 1
    }, 8, ["modelValue", "title"]));
  }
}), Oe = /* @__PURE__ */ U({
  __name: "EditAssignedView",
  props: {
    id: {},
    pluginName: {}
  },
  setup(r) {
    const n = r, { t: c } = L(), M = k(n.pluginName), { selectedAttributeSet: w } = R(M), { attributeSetsApi: S } = M, T = fe(), s = V(), l = V(!1), i = V(), d = V(!1);
    let f = [];
    const m = P(() => {
      var u;
      const a = (u = w.value) != null && u.readOnly ? {
        createActions: !1,
        tableActions: void 0,
        rowSelection: ge.NONE
      } : void 0, t = n.id ? void 0 : { data: [], isLoading: !0 };
      return {
        data: S.getAttributes,
        ...Ve,
        ...a,
        ...t,
        customQuery: { setId: n.id }
      };
    }), {
      table: h,
      showError: p,
      isLoading: b,
      isEditMode: y,
      showChangeDialog: g,
      onAddRow: X,
      onEditRow: Y,
      onDeleteRows: Z,
      batchApiCalls: x
    } = he({
      addApi: S.addAttributes,
      editApi: S.updateAttribute,
      deleteApi: S.removeAttributes
    });
    function ee(a) {
      a.dhAttribute.dataTypeInstance.attributeSetConfigAllowed && (s.value = a, y.value = !0, g.value = !0);
    }
    function te() {
      y.value = !1, g.value = !0;
    }
    async function ae(a) {
      const t = a.reduce(
        (o, { attributeId: _, requireFlag: oe }) => (o[+!!oe].push(_), o),
        { 0: [], 1: [] }
      ), u = Object.entries(t).filter(([, o]) => o.length).map(([o, _]) => [
        {
          dhAttributeSetId: n.id,
          dhAttributeIds: _,
          requireFlag: o === "1"
        }
      ]);
      await x(X, u, !1), p.value || (g.value = !1);
    }
    async function ie(a) {
      await Y(a), p.value || (g.value = !1);
    }
    async function le(a) {
      var t;
      try {
        b.value = !0, i.value = await B.getMaps({
          attributeId: a.dhAttribute.id,
          attributeSetId: n.id,
          includeSourcePortMaps: !1
        });
        const u = (t = i.value.etlAttributeSetMap) == null ? void 0 : t.destinationAttributeSet;
        if (u != null && u.parentSetId) {
          T.push({
            message: c(
              "datahub-ui.dh-shared.mappingsCannotBeCreatedForInheritedAttributeSets",
              {
                parentSetName: u.parentSetName,
                parentSetId: u.parentSetId
              }
            ),
            type: G.error,
            timeout: 1e4
          });
          return;
        }
        l.value = !0;
      } finally {
        b.value = !1;
      }
    }
    async function ue(a, t) {
      var u;
      switch (f = t, a) {
        case N.DELETE:
          x(
            Z,
            [[n.id, t.map(({ attributeId: o }) => o)]],
            !1
          );
          return;
        case N.ADD_MAPPING:
        case N.EDIT_MAPPING:
          return y.value = a === N.EDIT_MAPPING, le(t[0]);
        case N.DELETE_MAPPING:
          await B.deleteCalculatedAttributeMap(
            n.id,
            t[0].attributeId
          ), (u = h.value) == null || u.reloadTable();
          return;
      }
    }
    function ne() {
      T.push({
        message: c("datahub-ui.dh-shared.TriggeringRecalc"),
        type: G.success
      });
      const { destinationAttributeSet: a, id: t } = i.value.etlAttributeSetMap, u = {
        entityTypeId: a.entityType.id,
        attributeSetId: a.id,
        etlAttributeSetMapId: t,
        destinationAttributeId: f[0].dhAttribute.id,
        destinationLanguageId: 0,
        destinationScopeId: 0
      };
      B.recalculateEntityFields(u);
    }
    async function se(a) {
      var t;
      try {
        b.value = !0, y.value ? await q.update(a) : await q.create(a), p.value || (l.value = !1, d.value = !0);
      } catch (u) {
        console.error(u);
      } finally {
        (t = h.value) == null || t.reloadTable(), b.value = !1;
      }
    }
    return (a, t) => (E(), I(e(ye), j({
      ref_key: "table",
      ref: h,
      "disable-actions": e(b)
    }, m.value, {
      onAction: ue,
      onClickedRow: ee,
      onCreate: te
    }), {
      modals: v(() => {
        var u;
        return [
          e(g) ? (E(), re(me, { key: 0 }, [
            e(y) ? (E(), I(we, {
              key: 0,
              modelValue: e(g),
              "onUpdate:modelValue": t[0] || (t[0] = (o) => F(g) ? g.value = o : null),
              "attribute-map": s.value,
              "plugin-name": a.pluginName,
              onSave: t[1] || (t[1] = (o) => ie({ ...o, id: s.value.id }))
            }, null, 8, ["modelValue", "attribute-map", "plugin-name"])) : (E(), I(Ne, {
              key: 1,
              modelValue: e(g),
              "onUpdate:modelValue": t[2] || (t[2] = (o) => F(g) ? g.value = o : null),
              "plugin-name": a.pluginName,
              onSave: ae
            }, null, 8, ["modelValue", "plugin-name"]))
          ], 64)) : l.value ? (E(), I(Te, {
            key: 1,
            id: Number((u = i.value.etlAttributeSetMap) == null ? void 0 : u.id),
            modelValue: l.value,
            "onUpdate:modelValue": t[3] || (t[3] = (o) => l.value = o),
            "attribute-map": i.value.etlAttributeMap,
            "destination-attribute": e(f)[0].dhAttribute,
            loading: e(b),
            onSave: se
          }, null, 8, ["id", "modelValue", "attribute-map", "destination-attribute", "loading"])) : Q("", !0),
          A(e(Se), {
            modelValue: d.value,
            "onUpdate:modelValue": t[4] || (t[4] = (o) => d.value = o),
            title: e(c)("datahub-ui.dh-shared.recalculateExistingEntities"),
            confirm: e(c)("datahub-ui.dh-shared.recalculate"),
            message: e(c)("datahub-ui.dh-shared.recalculateMessage"),
            "min-width": "600px",
            "max-width": "800px",
            onConfirm: ne
          }, null, 8, ["modelValue", "title", "confirm", "message"])
        ];
      }),
      _: 1
    }, 16, ["disable-actions"]));
  }
});
export {
  Oe as default
};
