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