import { jsxs as R, jsx as p } from "react/jsx-runtime";
import { useRef as o, useCallback as s, useEffect as I, useMemo as b, createContext as A, useContext as L } from "react";
import { useRequestPermissions as M } from "../hooks/util/useRequestPermissions.js";
const v = A(null);
function U() {
  const f = L(v);
  if (!f)
    throw new Error(
      "useImagePickerContext must be used within an ImagePickerProvider"
    );
  return f;
}
const E = () => window?.minisParams?.platform === "android";
function F({ children: f }) {
  const C = o(null), h = o(null), g = o(null), n = o(null), e = o(null), a = o(null), { requestPermission: d } = M(), t = s(() => {
    if (a.current) {
      const { input: l, handler: c } = a.current;
      l.removeEventListener("cancel", c), a.current = null;
    }
  }, []), i = s(() => {
    e.current && (e.current(
      new Error("New file picker opened before previous completed")
    ), n.current = null, e.current = null);
  }, []), m = s(
    (l) => {
      const c = l.target.files?.[0];
      c && n.current && (n.current(c), n.current = null, e.current = null, t()), l.target.value = "";
    },
    [t]
  ), w = s(() => new Promise((l, c) => {
    i(), t(), n.current = l, e.current = c;
    const r = C.current;
    if (!r) {
      c(new Error("Gallery input not found")), n.current = null, e.current = null;
      return;
    }
    const u = () => {
      e.current && (e.current(new Error("User cancelled file selection")), n.current = null, e.current = null), t();
    };
    r.addEventListener("cancel", u), a.current = { input: r, handler: u }, E() ? d({ permission: "CAMERA" }).then(() => {
      r.click();
    }).catch(() => {
      r.click();
    }) : r.click();
  }), [i, t, d]), P = s(
    (l = "back") => new Promise((c, r) => {
      i(), t(), n.current = c, e.current = r;
      const u = l === "front" ? h.current : g.current;
      if (!u) {
        r(new Error("Camera input not found")), n.current = null, e.current = null;
        return;
      }
      const k = () => {
        e.current && (e.current(new Error("User cancelled camera")), n.current = null, e.current = null), t();
      };
      u.addEventListener("cancel", k), a.current = { input: u, handler: k }, E() ? d({ permission: "CAMERA" }).then(({ granted: x }) => {
        x ? u.click() : (r(new Error("Camera permission not granted")), n.current = null, e.current = null);
      }).catch(() => {
        r(new Error("Camera permission not granted")), n.current = null, e.current = null;
      }) : u.click();
    }),
    [i, t, d]
  );
  I(() => () => {
    i(), t();
  }, [i, t]);
  const y = b(
    () => ({
      openCamera: P,
      openGallery: w
    }),
    [P, w]
  );
  return /* @__PURE__ */ R(v.Provider, { value: y, children: [
    f,
    /* @__PURE__ */ p(
      "input",
      {
        ref: C,
        type: "file",
        accept: "image/*",
        onChange: m,
        style: { display: "none" },
        "aria-hidden": "true"
      }
    ),
    /* @__PURE__ */ p(
      "input",
      {
        ref: h,
        type: "file",
        accept: "image/*",
        capture: "user",
        onChange: m,
        style: { display: "none" },
        "aria-hidden": "true"
      }
    ),
    /* @__PURE__ */ p(
      "input",
      {
        ref: g,
        type: "file",
        accept: "image/*",
        capture: "environment",
        onChange: m,
        style: { display: "none" },
        "aria-hidden": "true"
      }
    )
  ] });
}
export {
  F as ImagePickerProvider,
  U as useImagePickerContext
};
//# sourceMappingURL=ImagePickerProvider.js.map
