import { defineComponent as X, mergeModels as G, useModel as q, ref as c, createElementBlock as w, openBlock as p, createElementVNode as g, createBlock as v, createCommentVNode as f, toDisplayString as z, normalizeClass as J, renderSlot as le, createVNode as u, unref as e, withModifiers as ie, withCtx as b, createTextVNode as oe, computed as R, watch as F, Fragment as ue, renderList as ne } from "vue";
import { useI18n as se } from "vue-i18n";
import { storeToRefs as de } from "pinia";
import { a as S, I as O, X as Q, o as W, h as re, i as pe, c as h, d as me } from "./start-CE9SawgU.js";
import { C as ce } from "./header-Bn1EKnBE.js";
import { c as be, M as ve, e as K } from "./topBarRoutes-CBd5qLxn.js";
import { _ as ge, o as fe, p as Ve } from "./DatahubDataTypeEditor.vue_vue_type_script_setup_true_lang-Blx83HK-.js";
const Ae = { class: "selector" }, ye = { class: "selector__wrapper" }, he = { class: "selector__header" }, Se = { class: "d-flex align-center" }, Me = {
  key: 1,
  class: "mr-2"
}, Ie = {
  key: 0,
  class: "text-alert"
}, _e = /* @__PURE__ */ X({
  __name: "DataSelector",
  props: /* @__PURE__ */ G({
    title: {},
    items: {},
    loading: { type: Boolean },
    selected: { type: Boolean },
    port: {}
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(M) {
    const E = M, a = q(M, "modelValue"), A = c(!1), I = c();
    function B() {
      I.value = {
        name: E.title,
        dataTypeInstance: a.value?.dataTypeInstance
      }, A.value = !0;
    }
    function N() {
      A.value = !1;
    }
    return (n, m) => (p(), w("div", Ae, [
      g("div", ye, [
        g("div", he, z(n.title), 1),
        g("div", {
          class: J(["selector__value", { selector__active: n.selected }])
        }, [
          g("div", Se, [
            u(e(S), {
              modelValue: a.value,
              "onUpdate:modelValue": m[0] || (m[0] = (V) => a.value = V),
              items: n.items,
              loading: n.loading,
              "item-title": "name",
              "item-value": "id",
              "return-object": "",
              clearable: "",
              onClick: m[1] || (m[1] = ie(() => {
              }, ["stop"]))
            }, null, 8, ["modelValue", "items", "loading"]),
            a.value?.sourceType === 2 ? (p(), v(e(O), {
              key: 0,
              icon: "lucide:pencil",
              variant: "text",
              class: "ml-1",
              onClick: B
            })) : f("", !0)
          ]),
          le(n.$slots, "default", {}, void 0, !0)
        ], 2)
      ]),
      m[4] || (m[4] = g("div", { class: "selector__arrow" }, null, -1)),
      a.value && A.value ? (p(), v(e(Q), {
        key: 0,
        modelValue: A.value,
        "onUpdate:modelValue": m[3] || (m[3] = (V) => A.value = V),
        title: n.$t("datahub-ui.etl.enterValue"),
        "max-width": "600px"
      }, {
        actions: b(() => [
          u(e(O), {
            text: n.$t("datahub-ui.dh-shared.save"),
            color: "primary",
            onClick: N
          }, null, 8, ["text"])
        ]),
        default: b(() => [
          u(ge, {
            modelValue: a.value.sourceValue,
            "onUpdate:modelValue": m[2] || (m[2] = (V) => a.value.sourceValue = V),
            label: n.title,
            attribute: I.value,
            "data-type-instance": a.value.dataTypeInstance
          }, null, 8, ["modelValue", "label", "attribute", "data-type-instance"])
        ]),
        _: 1
      }, 8, ["modelValue", "title"])) : f("", !0),
      n.port ? (p(), w("span", Me, [
        oe(z(n.port.name) + " ", 1),
        n.port.optional ? f("", !0) : (p(), w("span", Ie, "*"))
      ])) : f("", !0)
    ]));
  }
}), j = /* @__PURE__ */ W(_e, [["__scopeId", "data-v-1c5bdde0"]]), ke = { class: "mapping__wrapper ma-0 mt-2" }, Te = { class: "mapping__scroller" }, Ce = { class: "mapping__content" }, $e = { class: "mapping__target" }, Ne = /* @__PURE__ */ X({
  __name: "EditAttributeMapping",
  props: /* @__PURE__ */ G({
    id: {},
    attributeMap: {},
    destinationAttribute: {},
    loading: { type: Boolean },
    pluginName: {}
  }, {
    modelValue: { type: Boolean },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ G(["save"], ["update:modelValue"]),
  setup(M, { emit: E }) {
    const a = M, A = E, I = q(M, "modelValue"), B = be(a.pluginName), { languages: N, scopes: n, isLoadingLanguages: m, isLoadingScopes: V } = de(
      B
    ), U = c(!1), _ = c({}), L = c(), D = c(), { t: y } = se(), k = c(), o = c(), P = c([]), T = c(!1), s = c([]), C = c([]), $ = c(!1), Y = R(
      () => Object.entries(ce).map(([t, l]) => ({
        value: String(t),
        title: y(l)
      }))
    ), Z = R(
      () => y("datahub-ui.dh-table.editMapping", {
        name: a.destinationAttribute?.name || ""
      })
    ), x = R(
      () => !a.loading && !T.value && !$.value && o.value && (!o.value.ports?.length || o.value.ports.every(
        ({ optional: t }, l) => t || s.value[l]
      ))
    );
    async function ee() {
      try {
        L.value = a.attributeMap?.destinationLanguage?.id, D.value = a.attributeMap?.destinationScope?.id, _.value = { ...a.attributeMap?.configuration }, k.value = a.attributeMap?.overrideChangeTrackingMode == null ? "-1" : String(a.attributeMap?.overrideChangeTrackingMode), P.value = [], C.value = [], s.value = [], T.value = !0;
        const { data: t } = await K.getPossibleExtensions({
          dataType: a.destinationAttribute.dataType,
          destinationAttributeId: a.destinationAttribute.id,
          etlAttributeSetMapId: a.id
        });
        P.value = t, o.value = t.find(
          ({ id: l }) => l === a.attributeMap?.extension
        );
      } finally {
        T.value = !1;
      }
    }
    async function te(t) {
      if (_.value = { ...a.attributeMap?.configuration }, s.value = [], C.value = [], !!t)
        try {
          $.value = !0;
          const l = await Promise.all(
            t.ports.map(
              ({ sourceDataTypes: i }) => K.getPossibleSourceAttributes({
                etlSetMapId: a.id,
                dataTypes: i,
                extension: t.id,
                destinationAttributeId: a.destinationAttribute.id
              })
            )
          );
          C.value = l.map(
            ({ data: i }, d) => i.map((r) => ({
              ...r,
              portName: t.ports[d].id,
              sourceType: fe(r)
            }))
          ), t.id === a.attributeMap?.extension && (s.value = t.ports?.map((i, d) => {
            const r = a.attributeMap.sourcePorts?.find(
              ({ portName: H }) => i.portName === H
            );
            return r ? {
              ...C.value[d].find(
                ({ id: H }) => H === Ve(r)
              ),
              sourceValue: r.sourceValue,
              dhSourceAttributeLanguageId: r.sourceLanguage?.id,
              dhSourceAttributeScopeId: r.sourceScope?.id
            } : null;
          }) ?? []);
        } finally {
          $.value = !1;
        }
    }
    function ae() {
      const t = o.value.ports.length ? s.value.map((l, i) => l ? {
        dhSourceAttributeId: l.id,
        portName: o.value.ports[i].portName,
        sourceType: l.sourceType,
        sourceValue: l.sourceValue,
        dhSourceAttributeLanguageId: l.dhSourceAttributeLanguageId ?? 0,
        dhSourceAttributeScopeId: l.dhSourceAttributeScopeId ?? 0
      } : null).filter(Boolean) : [{ sourceType: 2, portName: "p0" }];
      A("save", {
        etlAttributeSetMapId: a.id,
        dhDestinationAttributeId: String(a.destinationAttribute.id),
        destinationType: Number(isNaN(a.destinationAttribute.id)),
        id: a.attributeMap?.id,
        extension: o.value.id,
        overrideChangeTrackingMode: k.value == null ? -1 : Number(k.value),
        sources: t,
        dhDestinationAttributeLanguageId: L.value,
        dhDestinationAttributeScopeId: D.value,
        config: _.value
      });
    }
    return F(o, te), F(() => a.id, ee, { immediate: !0 }), (t, l) => (p(), v(e(Q), {
      modelValue: I.value,
      "onUpdate:modelValue": l[7] || (l[7] = (i) => I.value = i),
      title: Z.value,
      width: "95%",
      height: "95%"
    }, {
      actions: b(() => [
        u(e(O), {
          disabled: !x.value,
          text: t.$t("common.saveChanges"),
          color: "primary",
          onClick: ae
        }, null, 8, ["disabled", "text"])
      ]),
      default: b(() => [
        u(e(re), {
          fluid: "",
          class: "d-flex flex-column overflow-hidden h-100 pt-2 pa-0"
        }, {
          default: b(() => [
            u(e(pe), { class: "flex-grow-0" }, {
              default: b(() => [
                u(e(h), {
                  md: "6",
                  lg: "4"
                }, {
                  default: b(() => [
                    u(e(S), {
                      modelValue: k.value,
                      "onUpdate:modelValue": l[0] || (l[0] = (i) => k.value = i),
                      items: Y.value,
                      label: e(y)("datahub-ui.dh-table.changeTracking")
                    }, null, 8, ["modelValue", "items", "label"])
                  ]),
                  _: 1
                })
              ]),
              _: 1
            }),
            g("div", ke, [
              g("div", Te, [
                g("div", Ce, [
                  g("div", {
                    class: J(["mapping__source", { hidden: !o.value && !t.attributeMap }])
                  }, [
                    T.value || $.value ? (p(), v(j, {
                      key: 0,
                      title: t.$t("datahub-ui.dh-shared.sourceAttribute"),
                      items: [],
                      loading: ""
                    }, null, 8, ["title"])) : (p(!0), w(ue, { key: 1 }, ne(C.value, (i, d) => (p(), v(j, {
                      key: d,
                      modelValue: s.value[d],
                      "onUpdate:modelValue": (r) => s.value[d] = r,
                      title: t.$t("datahub-ui.dh-shared.sourceAttribute"),
                      items: i,
                      loading: $.value,
                      port: o.value.ports[d],
                      class: "mb-2"
                    }, {
                      default: b(() => [
                        s.value[d]?.multilanguage ? (p(), v(e(h), {
                          key: 0,
                          class: "px-0"
                        }, {
                          default: b(() => [
                            u(e(S), {
                              modelValue: s.value[d].dhSourceAttributeLanguageId,
                              "onUpdate:modelValue": (r) => s.value[d].dhSourceAttributeLanguageId = r,
                              items: e(N),
                              label: e(y)("Table.language"),
                              loading: e(m),
                              "item-title": "description",
                              "item-value": "id",
                              clearable: ""
                            }, null, 8, ["modelValue", "onUpdate:modelValue", "items", "label", "loading"])
                          ]),
                          _: 2
                        }, 1024)) : f("", !0),
                        s.value[d]?.scopable ? (p(), v(e(h), {
                          key: 1,
                          class: "px-0"
                        }, {
                          default: b(() => [
                            u(e(S), {
                              modelValue: s.value[d].dhSourceAttributeScopeId,
                              "onUpdate:modelValue": (r) => s.value[d].dhSourceAttributeScopeId = r,
                              items: e(n),
                              label: e(y)("Table.channel"),
                              loading: e(V),
                              "item-title": "name",
                              "item-value": "id",
                              clearable: ""
                            }, null, 8, ["modelValue", "onUpdate:modelValue", "items", "label", "loading"])
                          ]),
                          _: 2
                        }, 1024)) : f("", !0)
                      ]),
                      _: 2
                    }, 1032, ["modelValue", "onUpdate:modelValue", "title", "items", "loading", "port"]))), 128))
                  ], 2),
                  u(j, {
                    modelValue: o.value,
                    "onUpdate:modelValue": l[1] || (l[1] = (i) => o.value = i),
                    items: P.value,
                    title: t.$t("datahub-ui.dh-shared.extension"),
                    loading: T.value,
                    selected: U.value,
                    class: "mapping__extension",
                    onClick: l[2] || (l[2] = (i) => U.value = !!o.value?.configurationComponent)
                  }, null, 8, ["modelValue", "items", "title", "loading", "selected"]),
                  g("div", $e, [
                    u(e(h), { class: "px-0" }, {
                      default: b(() => [
                        u(e(me), {
                          "model-value": t.destinationAttribute.name,
                          label: t.$t("datahub-ui.dh-shared.targetAttribute"),
                          readonly: ""
                        }, null, 8, ["model-value", "label"])
                      ]),
                      _: 1
                    }),
                    t.destinationAttribute?.multilanguage ? (p(), v(e(h), {
                      key: 0,
                      class: "px-0"
                    }, {
                      default: b(() => [
                        u(e(S), {
                          modelValue: L.value,
                          "onUpdate:modelValue": l[3] || (l[3] = (i) => L.value = i),
                          items: e(N),
                          label: e(y)("Table.language"),
                          loading: e(m),
                          readonly: !!t.attributeMap,
                          "item-title": "description",
                          "item-value": "id",
                          clearable: !t.attributeMap
                        }, null, 8, ["modelValue", "items", "label", "loading", "readonly", "clearable"])
                      ]),
                      _: 1
                    })) : f("", !0),
                    t.destinationAttribute?.scopable ? (p(), v(e(h), {
                      key: 1,
                      class: "px-0"
                    }, {
                      default: b(() => [
                        u(e(S), {
                          modelValue: D.value,
                          "onUpdate:modelValue": l[4] || (l[4] = (i) => D.value = i),
                          items: e(n),
                          label: e(y)("Table.channel"),
                          loading: e(V),
                          readonly: !!t.attributeMap,
                          "item-title": "name",
                          "item-value": "id",
                          clearable: !t.attributeMap
                        }, null, 8, ["modelValue", "items", "label", "loading", "readonly", "clearable"])
                      ]),
                      _: 1
                    })) : f("", !0)
                  ])
                ])
              ])
            ]),
            o.value?.configurationComponent ? (p(), v(ve, {
              key: 0,
              modelValue: U.value,
              "onUpdate:modelValue": l[5] || (l[5] = (i) => U.value = i),
              config: _.value,
              "onUpdate:config": l[6] || (l[6] = (i) => _.value = i),
              "attribute-map": t.attributeMap,
              "destination-attribute": t.destinationAttribute,
              "source-attributes": s.value,
              "etl-attribute-set-map-id": t.id,
              extension: o.value
            }, null, 8, ["modelValue", "config", "attribute-map", "destination-attribute", "source-attributes", "etl-attribute-set-map-id", "extension"])) : f("", !0)
          ]),
          _: 1
        })
      ]),
      _: 1
    }, 8, ["modelValue", "title"]));
  }
}), Re = /* @__PURE__ */ W(Ne, [["__scopeId", "data-v-93091ed6"]]);
export {
  Re as E
};
