import { P as r, bh as u, V as o, a0 as t, bZ as O, a as v, cf as _, bv as R, bd as A, bL as W, bB as q, cg as G, ch as X, w as L, be as I, b9 as P, ci as H, cj as J, ba as Y, bb as Z, U as K, bi as g, bj as x, bg as y, bk as Q, bl as ee, bY as te, ck as F, cl as c, bn as ie, bo as C, cm as le, bp as ae, cn as ne, co as oe, ca as re, cb as se, cp as $, cq as T, bD as w, cr as z, c1 as M, bE as ce, cs as de } from "./684d2f28.js";
import { useContext as V, useState as fe, useEffect as ue } from "react";
import { formDataChildren as me, FormDataPropertyView as pe } from "./00dac55d.js";
import "react-dom";
const U = de((e) => {
  var l;
  if (typeof e == "number")
    return e;
  if (typeof e == "string") {
    const a = e.trim();
    if (a === "")
      return 0;
    const s = Number(a);
    if (!M.isNaN(s))
      return s;
    const d = ["bytes", "kb", "mb", "gb", "tb"];
    if (new RegExp("^\\d+\\s*[kmgt]b$", "i").test(a)) {
      const m = parseInt(((l = a.match("^\\d+")) == null ? void 0 : l[0]) ?? "", 10), i = d.findIndex((h) => a.search(new RegExp(h, "i")) !== -1);
      return m * Math.pow(1024, i);
    }
  }
  throw new TypeError(t("file.typeErrorMsg", {
    value: typeof e
  }));
}), N = /* @__PURE__ */ r(u, {
  children: [/* @__PURE__ */ o("div", {
    children: t("file.parsedValueTooltip1")
  }), /* @__PURE__ */ o("div", {
    children: t("file.parsedValueTooltip2")
  })]
}), be = [O, {
  label: t("event.parse"),
  value: "parse",
  description: t("event.parseDesc")
}], he = {
  minSize: U,
  maxSize: U,
  maxFiles: ne
}, ge = {
  value: v([]),
  files: v([]),
  fileType: _,
  showUploadList: R.DEFAULT_TRUE,
  disabled: A,
  onEvent: W(be),
  style: q(G),
  parseFiles: X,
  parsedValue: v([]),
  prefixIcon: L(I, "/icon:solid/arrow-up-from-bracket"),
  suffixIcon: I,
  ...he
}, xe = (e) => [e.minSize.propertyView({
  label: t("file.minSize"),
  placeholder: "1kb",
  tooltip: t("file.minSizeTooltip")
}), e.maxSize.propertyView({
  label: t("file.maxSize"),
  placeholder: "10kb",
  tooltip: t("file.maxSizeTooltip")
})], ye = (e) => ({
  accept: e.fileType.toString(),
  multiple: e.uploadType === "multiple",
  directory: e.uploadType === "directory",
  showUploadList: e.showUploadList,
  customRequest: (l) => l.onSuccess && l.onSuccess({})
  // Override the default upload logic and do not upload to the specified server
}), we = (e) => oe`
    .ant-btn {
      border-radius: ${e.radius};
      margin: ${e.margin};	
      padding: ${e.padding};	
      width: ${re(e.margin)};	
      height: ${se(e.margin)};
      font-family:${e.fontFamily};
      font-size:${e.textSize};
      font-weight:${e.textWeight};
      font-style:${e.fontStyle};
      border-width:${e.borderWidth};
    }

    .ant-btn:not(:disabled) {
      border-color: ${e.border};
      background-color: ${e.background};
      color: ${e.text};

      &:hover,
      &:focus {
        border-color: ${e.accent};
        color: ${e.accent};
      }

      &:active {
        border-color: ${$(e.accent, 0.1)};
        color: ${$(e.accent, 0.1)};
      }
    }
  `, Ve = P(T)`
  .ant-upload,
  .ant-btn {
    width: 100%;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 6px;

    > span {
      overflow: hidden;
      display: inline-flex;
      justify-content: flex-start;
      align-items: center;
      gap: 6px;
      min-height: 1px;
    }
  }

  ${(e) => e.$style && we(e.$style)}
`, k = P.span`
  display: flex;
`;
function Se(e) {
  return Promise.all(e.map((l) => {
    var a;
    return ((a = l.originFileObj) == null ? void 0 : a.arrayBuffer().then((s) => H.Buffer.from(s).toString("base64")).catch(() => null)) ?? null;
  }));
}
function ve(e) {
  return Promise.all(e.map(async (l) => {
    var s;
    const a = await import("./2112abba.js");
    return ((s = l.originFileObj) == null ? void 0 : s.arrayBuffer().then((d) => {
      var i;
      const f = J.getExtension(((i = l.originFileObj) == null ? void 0 : i.type) ?? "");
      if (f === "xlsx" || f === "csv") {
        const h = a.read(d, {
          raw: !0,
          codepage: 65001
        });
        return a.utils.sheet_to_json(h.Sheets[h.SheetNames[0]], {
          raw: !1
        });
      }
      const m = new TextDecoder("utf-8").decode(d);
      return m ? JSON.parse(m) : null;
    }).catch(() => null)) ?? null;
  }));
}
const Ce = (e) => {
  const {
    dispatch: l,
    files: a,
    style: s
  } = e, [d, f] = fe(a.map((i) => ({
    ...i,
    status: "done"
  })));
  ue(() => {
    a.length === 0 && d.length !== 0 && f([]);
  }, [a]);
  const m = w(e.prefixIcon) || !!e.text || w(e.suffixIcon);
  return /* @__PURE__ */ o(Ve, {
    ...ye(e),
    $style: s,
    fileList: d,
    beforeUpload: (i) => !i.size || i.size <= 0 ? (z.error(`${i.name} ` + t("file.fileEmptyErrorMsg")), T.LIST_IGNORE) : e.minSize && i.size < e.minSize || e.maxSize && i.size > e.maxSize ? (z.error(`${i.name} ` + t("file.fileSizeExceedErrorMsg")), T.LIST_IGNORE) : !0,
    onChange: (i) => {
      if (i.fileList.filter((n) => n.status === "uploading").length !== 0) {
        f(i.fileList);
        return;
      }
      let p = e.maxFiles;
      e.uploadType === "single" ? p = 1 : e.maxFiles <= 0 && (p = 100);
      const b = i.fileList.filter((n) => n.status === "done");
      if (i.file.status === "removed") {
        const n = e.files.findIndex((S) => S.uid === i.file.uid);
        l(F({
          value: c([...e.value.slice(0, n), ...e.value.slice(n + 1)], !1),
          files: c([...e.files.slice(0, n), ...e.files.slice(n + 1)], !1),
          parsedValue: c([...e.parsedValue.slice(0, n), ...e.parsedValue.slice(n + 1)], !1)
        })), e.onEvent("change");
      } else {
        const n = Math.min(e.value.length, b.length), S = Math.min(e.parsedValue.length, b.length);
        Promise.all([Se(b.slice(n)), ve(b.slice(S))]).then(([D, j]) => {
          l(F({
            value: c([...e.value, ...D].slice(-p), !1),
            files: c(b.map((B) => M.pick(B, ["uid", "name", "type", "size", "lastModified"])).slice(-p), !1),
            ...e.parseFiles ? {
              parsedValue: c([...e.parsedValue, ...j].slice(-p), !1)
            } : {}
          })), e.onEvent("change"), e.onEvent("parse");
        });
      }
      f(b.slice(-p));
    },
    children: /* @__PURE__ */ o(ce, {
      disabled: e.disabled,
      children: m && /* @__PURE__ */ r("span", {
        children: [w(e.prefixIcon) && /* @__PURE__ */ o(k, {
          children: e.prefixIcon
        }), !!e.text && e.text, w(e.suffixIcon) && /* @__PURE__ */ o(k, {
          children: e.suffixIcon
        })]
      })
    })
  });
}, Fe = [{
  label: t("file.single"),
  value: "single"
}, {
  label: t("file.multiple"),
  value: "multiple"
}, {
  label: t("file.directory"),
  value: "directory"
}], Te = {
  text: L(Y, t("file.upload")),
  uploadType: Z(Fe, "single"),
  ...ge,
  ...me
};
let E = new K(Te, (e, l) => /* @__PURE__ */ o(Ce, {
  ...e,
  dispatch: l
})).setPropertyViewFn((e) => /* @__PURE__ */ r(u, {
  children: [/* @__PURE__ */ r(g, {
    name: x.basic,
    children: [e.text.propertyView({
      label: t("text")
    }), e.uploadType.propertyView({
      label: t("file.uploadType")
    })]
  }), /* @__PURE__ */ o(pe, {
    ...e
  }), (V(y).editorModeStatus === "logic" || V(y).editorModeStatus === "both") && /* @__PURE__ */ r(u, {
    children: [/* @__PURE__ */ r(g, {
      name: x.validation,
      children: [e.uploadType.getView() !== "single" && e.maxFiles.propertyView({
        label: t("file.maxFiles")
      }), xe(e)]
    }), /* @__PURE__ */ r(g, {
      name: x.interaction,
      children: [e.onEvent.getPropertyView(), Q(e), ee(e)]
    }), /* @__PURE__ */ r(g, {
      name: x.advanced,
      children: [e.fileType.propertyView({
        label: t("file.fileType"),
        placeholder: '[".png"]',
        tooltip: /* @__PURE__ */ r(u, {
          children: [t("file.reference"), " ", /* @__PURE__ */ o("a", {
            href: t("file.fileTypeTooltipUrl"),
            target: "_blank",
            rel: "noreferrer",
            children: t("file.fileTypeTooltip")
          })]
        })
      }), e.prefixIcon.propertyView({
        label: t("button.prefixIcon")
      }), e.suffixIcon.propertyView({
        label: t("button.suffixIcon")
      }), e.showUploadList.propertyView({
        label: t("file.showUploadList")
      }), e.parseFiles.propertyView({
        label: t("file.parseFiles"),
        tooltip: N,
        placement: "right"
      })]
    })]
  }), (V(y).editorModeStatus === "layout" || V(y).editorModeStatus === "both") && /* @__PURE__ */ o(u, {
    children: /* @__PURE__ */ o(g, {
      name: x.style,
      children: e.style.getPropertyView()
    })
  })]
})).build();
E = te(E, [{
  method: {
    name: "clearValue",
    description: t("file.clearValueDesc"),
    params: []
  },
  execute: (e) => e.dispatch(F({
    value: c([], !1),
    files: c([], !1),
    parsedValue: c([], !1)
  }))
}]);
const Ue = ie(E, [new C("value", t("file.filesValueDesc")), new C("files", /* @__PURE__ */ r(u, {
  children: [t("file.filesDesc"), (() => {
    const e = le("file");
    if (e)
      return /* @__PURE__ */ r(u, {
        children: [" ", /* @__PURE__ */ o("a", {
          href: e,
          target: "_blank",
          rel: "noreferrer",
          children: t("uiComp.fileUploadCompName")
        })]
      });
  })()]
})), new C("parsedValue", N), ...ae]);
export {
  Ue as FileComp,
  ve as resolveParsedValue,
  Se as resolveValue
};
