import { defineComponent as L, mergeModels as _, useModel as B, ref as I, computed as y, watch as N, createBlock as z, openBlock as S, unref as n, withCtx as r, createVNode as d, mergeProps as O, withModifiers as c } from "vue";
import { useI18n as P } from "vue-i18n";
import { at as j, au as G, u as H, av as q, i as J, E as f, aw as K, ap as Q, b as v, ar as W, as as X, l as Y } from "./start-BYfL32Oe.mjs";
import "pinia";
import "vue-router";
import "./header-Droxy2Kd.mjs";
import "./moment-h96o7c8I.mjs";
import { V as Z } from "./constants-CwfsvyFM.mjs";
import "./IsActiveStatusCell-DlvB6_E-.mjs";
import "@vuelidate/validators";
import "./MultiLangScopeEditorsList.vue_vue_type_script_setup_true_lang-CWQeDnSm.mjs";
import "./DatahubDataTypeCompletenessRenderer.vue_vue_type_script_setup_true_lang-Btvu_WeF.mjs";
import "./topBarRoutes-0VuQ_i6D.mjs";
import "./VariantToVariantExtension-WcLvPNqG.mjs";
import "./DatahubDataTypeVariantValueEditor-f1gR3QCx.mjs";
import { i as ee } from "./DatahubDataTypeEditor.vue_vue_type_script_setup_true_lang-WxvRaHzL.mjs";
import { b as E } from "./file-value.json_vue_type_i18n_index_0_src_true_lang-BPEd6MKW.mjs";
const V = {
  hideGlobalSearch: !0,
  minRowCount: 0,
  canReorderRows: !1,
  hidePagination: !0,
  canReorderHeaders: !1,
  columns: [
    {
      id: "name",
      header: "common.name"
    },
    {
      id: "description",
      header: "common.description"
    },
    {
      id: "fileTags",
      header: "datahub-plugin.file-value.fileTags"
    },
    {
      id: "timestamp",
      header: "datahub-plugin.file-value.date",
      accessor: ({ timestamp: s }) => ee(s)
    },
    {
      id: "size",
      header: "datahub-plugin.file-value.size",
      accessor: ({ size: s }) => j(s, 1)
    },
    {
      id: G,
      width: 200
    }
  ]
}, D = /* @__PURE__ */ L({
  __name: "DatahubDataTypeFileValueEditor",
  props: /* @__PURE__ */ _({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    baseFolder: { default: "/.ecmfileimage_tmp/" },
    uploadEndpoint: { default: "Actindo.Modules.Actindo.ECM.Upload.upload" },
    previewEndpoint: { default: "Actindo.Modules.Actindo.ECM.Download.preview?path=" },
    downloadEndpoint: { default: "Actindo.Modules.Actindo.ECM.Download.download?path=" }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ _(["validated", "update:model-value"], ["update:modelValue"]),
  setup(s, { emit: w }) {
    const i = s, F = w, u = B(s, "modelValue"), { t: p } = P(), { root: T, dispatchEvent: A } = H(Z), g = I(), m = y(
      () => {
        var a, e;
        return ((e = (a = u.value) == null ? void 0 : a.files) == null ? void 0 : e.filter(({ permanentDelete: o }) => !o)) ?? [];
      }
    ), h = ({ id: a }, e) => a ? Q(W(), e, a) : null, C = y(() => ({
      ...V,
      columns: V.columns.map(({ header: a, ...e }) => ({
        header: a ? p(a) : "",
        ...e
      })),
      data: m.value,
      readonly: i.readOnly,
      canRemove: !i.readOnly,
      tableName: i.label,
      isLoading: i.loading,
      hideNoDataDescription: !0
    })), U = () => ({
      id: null,
      name: null,
      fileName: null,
      description: null,
      fileTags: null
    });
    function M(a, e) {
      var o, l;
      u.value = {
        ...u.value,
        files: [
          ...((l = (o = u.value) == null ? void 0 : o.files) == null ? void 0 : l.filter(
            ({ permanentDelete: t }) => t
          )) ?? [],
          ...m.value,
          {
            id: a,
            name: e.name,
            fileName: e.name,
            description: "",
            fileTags: "",
            size: e.size,
            timestamp: Date.now()
          }
        ]
      };
    }
    function k({ rowIndex: a }) {
      m.value[a].permanentDelete = !0;
    }
    function R(a) {
      const e = document.createElement("iframe");
      e.style.visibility = "collapse", e.style.display = "none", document.body.append(e);
      const o = h(a, i.downloadEndpoint);
      e.contentDocument.write(`
<form action="${o}" method="POST">
  <input type="hidden" name="id" value="${a.id}">
</form>
`), e.contentDocument.forms[0].submit(), setTimeout(() => e.remove(), 2e3);
    }
    async function b(a) {
      for (const e of Array.from(a)) {
        const o = Math.random().toString(36).substring(2), l = encodeURIComponent(i.baseFolder);
        let t = `${i.uploadEndpoint}?id=${l}`;
        o && (t = `${t}&prefix=${o}`);
        const { data: $ } = await X(t, e);
        M(i.baseFolder + $, e);
      }
    }
    const x = () => {
      var a;
      return (a = g.value) == null ? void 0 : a.openDialog();
    };
    return N(
      m,
      (a) => {
        const e = !!(!a.length || a.every(({ permanentDelete: o, id: l }) => o || l));
        A({ data: e }), F("validated", e);
      },
      { deep: !0, immediate: !0 }
    ), (a, e) => (S(), z(n(q), {
      message: n(p)("datahub-plugin.file-value.dropFiles"),
      class: "uploader",
      onUpload: b
    }, {
      default: r(() => [
        d(n(J), O({
          ref_key: "root",
          ref: T
        }, C.value, {
          onCreate: U,
          onRemove: k
        }), {
          "cell-name": r(({ readonly: o, row: l }) => [
            d(n(v), {
              modelValue: l.name,
              "onUpdate:modelValue": (t) => l.name = t,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          "cell-description": r(({ readonly: o, row: l }) => [
            d(n(v), {
              modelValue: l.description,
              "onUpdate:modelValue": (t) => l.description = t,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          "cell-fileTags": r(({ readonly: o, row: l }) => [
            d(n(v), {
              modelValue: l.fileTags,
              "onUpdate:modelValue": (t) => l.fileTags = t,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          cellActions: r(({ readonly: o, row: l }) => [
            d(n(f), {
              icon: "cloud_download",
              variant: "text",
              color: "on_light",
              disabled: o,
              onClick: c((t) => R(l), ["stop"])
            }, null, 8, ["disabled", "onClick"]),
            d(n(f), {
              href: h(l, a.previewEndpoint),
              icon: "zoom_in",
              variant: "text",
              color: "on_light",
              target: "_blank",
              rel: "noreferrer noopener",
              disabled: o,
              download: "",
              onClick: e[0] || (e[0] = c(() => {
              }, ["stop"]))
            }, null, 8, ["href", "disabled"])
          ]),
          actions: r(({ readonly: o }) => [
            d(n(f), {
              text: n(p)("datahub-plugin.file-value.addFile"),
              disabled: o,
              "prepend-icon": "add",
              variant: "text",
              color: "primary",
              onClick: c(x, ["stop"])
            }, null, 8, ["text", "disabled"]),
            d(n(K), {
              ref_key: "inputRef",
              ref: g,
              onSelect: b
            }, null, 512)
          ]),
          _: 1
        }, 16)
      ]),
      _: 1
    }, 8, ["message"]));
  }
});
typeof E == "function" && E(D);
const be = /* @__PURE__ */ Y(D, [["__scopeId", "data-v-0c3c59e8"]]);
export {
  be as default
};
