import { jsx as t } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import A from "classnames";
import { useState as b, useMemo as Z, useCallback as R } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import $ from "../../../Typography/Typography.js";
import H from "../../../../../hooks/useTrackedRef.js";
import { DEFAULT_MAX_FILE_SIZE as C, DEFAULT_FILE_TYPES as J, validationErrors as l, BASE_CLASS as o } from "../constants.js";
import { TypographyVariant as M, TypographyElement as U } from "../../../Typography/types.js";
/* empty css                */
import { fixedForwardRef as K } from "../../../../../utils/preact/fixedForwardRef.js";
import ee from "../../../../../core/Context/preact/useCoreContext.js";
import { getUploadedFilesFromSource as re } from "../../../../../utils/file/upload.js";
import { Icon as N } from "../../../Icon/Icon.js";
import { Fragment as te } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/dist/preact.module.js";
import oe from "../../../../../utils/random/uniqueId.js";
import { isFunction as V } from "../../../../../utils/value/is.js";
const r = {
  dropzone: `${o}__dropzone`,
  dropzoneDisabled: `${o}__dropzone--disabled`,
  dropzoneDragOver: `${o}__dropzone--dragover`,
  dropzoneError: `${o}__dropzone--error`,
  label: `${o}__label`,
  labelDefault: `${o}__label-default-content`,
  labelIcon: `${o}__label-icon`,
  labelText: `${o}__label-text`,
  error: `${o}__error`,
  errorIcon: `${o}__error-icon`,
  errorText: `${o}__error-text`
}, ae = async (I) => new Promise((F, u) => {
  const s = new Image(), f = URL.createObjectURL(I), n = () => URL.revokeObjectURL(f);
  s.src = f, s.onerror = (D) => {
    n(), u(D);
  }, s.onload = () => {
    n(), F({ width: s.width, height: s.height });
  };
}), Fe = K((I, F) => {
  const {
    id: u,
    name: s,
    children: f,
    disabled: n = !1,
    required: D = !1,
    validDimensions: c,
    maxFileSize: h = C,
    allowedFileTypes: v = J,
    mapError: z,
    uploadFiles: S
  } = I, { i18n: T } = ee(), [_, Y] = b(""), [g, w] = b(), [q, y] = b(!1), E = !!_, P = s?.trim(), x = Z(() => u || oe(), [u]), L = H(F), k = (e) => {
    Array.from(e.dataTransfer?.types ?? []).some((d) => d === "Files") && (e.preventDefault(), y(!0));
  }, B = (e) => {
    e.preventDefault(), y(!1);
  }, j = (e) => {
    e.preventDefault(), y(!1), O(e.dataTransfer);
  }, G = (e) => {
    g && w(void 0), O(e.target);
  }, W = async (e) => {
    e.target !== document.activeElement && e.target?.checkValidity();
  }, Q = async (e) => {
    !_ && e.target.validity.valueMissing && m(l.FILE_REQUIRED);
  }, m = R(
    (e) => {
      const a = L.current;
      if (a) {
        const d = a.required;
        a.required = !1, a.setCustomValidity(e), Y(a.validationMessage ?? ""), a.required = d;
      }
    },
    [L]
  ), O = R(
    async (e) => {
      const a = re(e);
      if (a.length > 1)
        return m(l.TOO_MANY_FILES);
      try {
        const X = (await Promise.all(
          a.map(async (i) => {
            if (!c || !c?.width || !c?.height) return i;
            const p = await ae(i);
            if (c?.width !== p.width || c?.height !== p.height)
              throw l.INVALID_DIMENSIONS;
            return i;
          })
        )).filter((i) => {
          if (!v.includes(i.type))
            throw l.DISALLOWED_FILE_TYPE;
          const p = V(h) ? h(i.type) ?? C : h;
          if (i.size > p)
            throw w({ type: i.type, limit: p }), l.VERY_LARGE_FILE;
          return !0;
        });
        m(""), S(X);
      } catch (d) {
        switch (d) {
          case l.DISALLOWED_FILE_TYPE:
          case l.VERY_LARGE_FILE:
          case l.INVALID_DIMENSIONS:
            return m(d);
        }
      }
    },
    [v, h, m, S, c]
  );
  return /* @__PURE__ */ t(te, { children: [
    /* @__PURE__ */ t(
      "div",
      {
        role: "region",
        className: A(r.dropzone, {
          [r.dropzoneDisabled]: n,
          [r.dropzoneDragOver]: q,
          [r.dropzoneError]: E
        }),
        onDragOver: n ? void 0 : k,
        onDragLeave: n ? void 0 : B,
        onDrop: n ? void 0 : j,
        children: [
          /* @__PURE__ */ t(
            "input",
            {
              type: "file",
              className: "adyen-pe-visually-hidden",
              id: x,
              ref: L,
              name: P,
              disabled: n,
              required: D,
              accept: String(v),
              onBlur: W,
              onChange: G,
              onInvalid: Q,
              "aria-invalid": E,
              "data-testid": "dropzone-input"
            }
          ),
          /* @__PURE__ */ t("label", { "data-testid": "dropzone-label", className: r.label, htmlFor: x, children: f ?? /* @__PURE__ */ t("div", {
            className: A(r.labelDefault),
            // prettier-ignore
            children: [
              E ? /* @__PURE__ */ t(N, { name: "warning-filled", className: r.labelIcon }) : /* @__PURE__ */ t(N, { name: "upload", className: r.labelIcon }),
              /* @__PURE__ */ t($, { className: r.labelText, el: U.SPAN, variant: M.BODY, stronger: !0, children: T.get("common.inputs.file.labels.default") })
            ]
          }) })
        ]
      }
    ),
    E && /* @__PURE__ */ t("div", { className: r.error, children: [
      /* @__PURE__ */ t(N, { name: "cross-circle-fill", className: r.errorIcon }),
      /* @__PURE__ */ t($, { className: r.errorText, el: U.SPAN, variant: M.BODY, children: V(z) ? z(
        _,
        g ? { size: g.limit, type: g.type } : void 0
      ) : T.get(_) })
    ] })
  ] });
});
export {
  Fe as Dropzone,
  Fe as default
};
