import { defineComponent as B, mergeModels as y, useModel as z, ref as I, computed as _, watch as L, createBlock as N, openBlock as S, unref as t, withCtx as s, createVNode as d, mergeProps as O, withModifiers as c } from "vue";
import { useI18n as P } from "vue-i18n";
import { az as j, aA as G, u as H, aB as q, j as J, I as f, aC as K, aD as Q, d as g, aw as W, ay as X, o as Y } from "./start-CE9SawgU.js";
import "pinia";
import "./header-Bn1EKnBE.js";
import { V as Z } from "./constants-CwfsvyFM.js";
import "@vuelidate/validators";
import "./TreeView.vue_vue_type_style_index_0_scoped_0bd14ef7_lang-CZieIjgg.js";
import "./DatahubDataTypeCompletenessRenderer.vue_vue_type_script_setup_true_lang-5buor0oX.js";
import "./topBarRoutes-CBd5qLxn.js";
import "./IsActiveStatusCell-xz7XnAYP.js";
import "./VariantToVariantExtension-DUC3WSrH.js";
import "./DatahubDataTypeVariantValueEditor-9WYNJneN.js";
import "./dataTypes-DocW2t0g.js";
import { b as E } from "./file-value.json_vue_type_i18n_index_0_src_true_lang-CGpYp_qr.js";
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",
      renderer: "CellUnixDateTime"
    },
    {
      id: "size",
      header: "datahub-plugin.file-value.size",
      accessor: ({ size: r }) => j(r, 1)
    },
    {
      id: G,
      header: "",
      width: 200
    }
  ]
}, D = /* @__PURE__ */ B({
  __name: "DatahubDataTypeFileValueEditor",
  props: /* @__PURE__ */ y({
    modelValue: {},
    readOnly: { type: Boolean },
    attribute: {},
    massEditing: { type: Boolean },
    label: {},
    fieldName: {},
    dataTypeInstance: {},
    loading: { type: Boolean },
    succeeded: { 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__ */ y(["validated", "update:model-value", "initialized"], ["update:modelValue"]),
  setup(r, { emit: w }) {
    const i = r, C = w, u = z(r, "modelValue"), { t: p } = P(), { root: T, dispatchEvent: A } = H(Z), v = I(), m = _(
      () => u.value?.files?.filter(({ permanentDelete: a }) => !a) ?? []
    ), h = ({ id: a }, e) => a ? Q(W(), e, a) : null, F = _(() => ({
      ...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) {
      u.value = {
        ...u.value,
        files: [
          ...u.value?.files?.filter(
            ({ permanentDelete: o }) => o
          ) ?? [],
          ...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 n = `${i.uploadEndpoint}?id=${l}`;
        o && (n = `${n}&prefix=${o}`);
        const { data: $ } = await X(n, e);
        M(i.baseFolder + $, e);
      }
    }
    const x = () => v.value?.openDialog();
    return L(
      m,
      (a) => {
        const e = !!(!a.length || a.every(({ permanentDelete: o, id: l }) => o || l));
        A({ data: e }), C("validated", e);
      },
      { deep: !0, immediate: !0 }
    ), (a, e) => (S(), N(t(q), {
      message: t(p)("datahub-plugin.file-value.dropFiles"),
      class: "uploader",
      onUpload: b
    }, {
      default: s(() => [
        d(t(J), O({
          ref_key: "root",
          ref: T
        }, F.value, {
          onCreate: U,
          onRemove: k
        }), {
          "cell-name": s(({ readonly: o, row: l }) => [
            d(t(g), {
              modelValue: l.name,
              "onUpdate:modelValue": (n) => l.name = n,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          "cell-description": s(({ readonly: o, row: l }) => [
            d(t(g), {
              modelValue: l.description,
              "onUpdate:modelValue": (n) => l.description = n,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          "cell-fileTags": s(({ readonly: o, row: l }) => [
            d(t(g), {
              modelValue: l.fileTags,
              "onUpdate:modelValue": (n) => l.fileTags = n,
              readonly: o
            }, null, 8, ["modelValue", "onUpdate:modelValue", "readonly"])
          ]),
          cellActions: s(({ readonly: o, row: l }) => [
            d(t(f), {
              icon: "lucide:cloud-download",
              variant: "text",
              color: "on_light",
              disabled: o,
              onClick: c((n) => R(l), ["stop"])
            }, null, 8, ["disabled", "onClick"]),
            d(t(f), {
              href: h(l, a.previewEndpoint),
              icon: "lucide: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: s(({ readonly: o }) => [
            d(t(f), {
              text: t(p)("datahub-plugin.file-value.addFile"),
              disabled: o,
              "prepend-icon": "lucide:plus",
              variant: "text",
              color: "primary",
              onClick: c(x, ["stop"])
            }, null, 8, ["text", "disabled"]),
            d(t(K), {
              ref_key: "inputRef",
              ref: v,
              onSelect: b
            }, null, 512)
          ]),
          _: 1
        }, 16)
      ]),
      _: 1
    }, 8, ["message"]));
  }
});
typeof E == "function" && E(D);
const ge = /* @__PURE__ */ Y(D, [["__scopeId", "data-v-858aa0ca"]]);
export {
  ge as default
};
