import { jsx as P } from "react/jsx-runtime";
import { CONTENT_TYPE_IMAGE_ANY as h } from "./index-Document.types.js";
import { useRef as F, useEffect as M } from "react";
const C = (p) => {
  const {
    isOpen: a,
    openCallback: d,
    onFilesSelected: u,
    accept: y = [h],
    multiple: I = !1,
    mediaItemTransformers: s = [],
    mediaItemProcessors: f = [],
    mediaItemValidators: m = []
  } = p, o = F(null);
  M(() => {
    a && o.current && (o.current.value = "", o.current.click(), d());
  }, [a]);
  const T = async (r) => {
    const c = (i) => ({
      file: i,
      filePriority: "primary"
    }), l = Array.from(r.target.files ? r.target.files : []);
    if (!l.length)
      return;
    let e = l.map(c);
    if (s.length)
      for (const i of s)
        e && i && (e = await i.applyTransformer(e));
    if (!e) {
      n(null);
      return;
    }
    let t = g(e);
    if (f.length)
      for (const i of f)
        t && i && (t = await i.applyProcessor(t));
    if (!t) {
      n(null);
      return;
    }
    if (m.length)
      for (const i of m)
        t && i && (t = await i.applyValidator(t));
    n(t);
  }, g = (r) => r.map((l) => {
    const {
      file: e,
      filePriority: t
    } = l;
    return {
      file: e,
      filePriority: t,
      fileType: e.type,
      name: e instanceof File ? e.name : "blob",
      size: e.size,
      lastModified: e instanceof File ? e.lastModified : Date.now(),
      objectType: "MediaItemFile"
    };
  }), n = (r) => {
    r && u(r);
  };
  return P("input", {
    ref: o,
    type: "file",
    accept: y.join(","),
    onChange: T,
    style: {
      display: "none"
    },
    multiple: I
  });
};
export {
  C as MediaItemSelectInput
};
