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