var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellCamera/index.tsx
var pisellCamera_exports = {};
__export(pisellCamera_exports, {
  default: () => pisellCamera_default
});
module.exports = __toCommonJS(pisellCamera_exports);
var import_react = __toESM(require("react"));
var import_react_camera_pro = require("react-camera-pro");
var import_iconfont = __toESM(require("../iconfont"));
var import_locales = require("../../locales");
var import_pisellModal = __toESM(require("../../components/pisellModal"));
var import_index = require("./index.less");
var PisellCamera = (0, import_react.forwardRef)((props, ref) => {
  const cameraRef = (0, import_react.useRef)(null);
  const [open, setOpen] = (0, import_react.useState)(false);
  const [photo, setPhoto] = (0, import_react.useState)("");
  const [cameraAvailable, setCameraAvailable] = (0, import_react.useState)(null);
  const uploadFileRef = (0, import_react.useRef)(null);
  const base64ToFile = (base64String, filename) => {
    var _a;
    const arr = base64String.split(",");
    const mime = ((_a = arr[0].match(/:(.*?);/)) == null ? void 0 : _a[1]) || "";
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
  };
  const takePhoto = () => {
    var _a, _b;
    if (!cameraRef.current || !cameraRef.current.takePhoto) {
      return;
    }
    try {
      const photo2 = (_b = (_a = cameraRef.current) == null ? void 0 : _a.takePhoto) == null ? void 0 : _b.call(_a);
      const timestamp = Date.now();
      const filename = `photo-${timestamp}.jpg`;
      setPhoto(photo2);
      const file = base64ToFile(photo2, filename);
      const uploadFile = {
        uid: timestamp.toString(),
        name: filename,
        status: "uploading",
        url: photo2,
        // 用于预览
        originFileObj: file,
        // 原始文件对象，用于上传
        thumbUrl: photo2,
        lastModified: Date.now(),
        lastModifiedDate: /* @__PURE__ */ new Date(),
        type: "image/jpeg",
        size: file.size
      };
      console.log("takePhoto_uploadFile", uploadFile);
      uploadFileRef.current = uploadFile;
      return uploadFile;
    } catch (error) {
      console.error("takePhoto_error", error);
      uploadFileRef.current = null;
      setPhoto("");
      return null;
    }
  };
  const onOpen = () => {
    setCameraAvailable(null);
    setPhoto("");
    uploadFileRef.current = null;
    setOpen(true);
  };
  const onClose = () => {
    cameraRef.current = null;
    uploadFileRef.current = null;
    setOpen(false);
    setPhoto("");
    setCameraAvailable(null);
  };
  const onRetakePhoto = () => {
    setPhoto("");
  };
  const onUsePhoto = () => {
    var _a;
    (_a = props == null ? void 0 : props.onChange) == null ? void 0 : _a.call(props, uploadFileRef.current);
    onClose();
  };
  (0, import_react.useImperativeHandle)(ref, () => {
    return {
      open: onOpen,
      takePhoto,
      isCameraAvailable: () => cameraAvailable
    };
  });
  console.log("cameraAvailable", cameraAvailable);
  return /* @__PURE__ */ import_react.default.createElement(
    import_pisellModal.default,
    {
      open,
      onCancel: (e) => {
        e.stopPropagation();
        e.preventDefault();
        onClose();
      },
      header: false,
      footer: null,
      destroyOnClose: true,
      bodyStyle: { padding: 0 },
      width: "90vw",
      className: "pisell-camera-modal"
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-camera-container",
        onClick: (e) => {
          e.stopPropagation();
          e.preventDefault();
        }
      },
      cameraAvailable === null ? (
        // 检测中
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-loading" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-loading-text" }, (0, import_locales.getText)("pisell-camera-checking")))
      ) : cameraAvailable === false ? (
        // 无摄像头
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-no-camera" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-no-camera-icon" }, "📷"), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-no-camera-text" }, (0, import_locales.getText)("pisell-camera-no-camera-accessible")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-no-camera-desc" }, (0, import_locales.getText)("pisell-camera-no-camera-desc")))
      ) : null,
      /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
        import_react_camera_pro.Camera,
        {
          ref: cameraRef,
          facingMode: "environment",
          errorMessages: {
            // noCameraAccessible: getText('pisell-camera-no-camera-accessible'),
            // permissionDenied: getText('pisell-camera-permission-denied'),
            // switchCamera: getText('pisell-camera-switch-camera'),
            // canvas: getText('pisell-camera-canvas'),
          },
          numberOfCamerasCallback: (numberOfCameras) => {
            console.log("numberOfCameras", numberOfCameras);
            if (!cameraRef.current) {
              return;
            }
            if (numberOfCameras <= 0) {
              setTimeout(() => {
                setCameraAvailable((pre) => {
                  if (pre === true) {
                    return true;
                  }
                  return false;
                });
              }, 5e3);
            } else {
              setCameraAvailable(true);
            }
          }
        }
      ), cameraAvailable === true && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-action" }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-camera-action-item-wrap",
          onClick: (e) => {
            e.stopPropagation();
            e.preventDefault();
            takePhoto();
          }
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-action-item" }, /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pisell2-camera-01", style: { fontSize: 32 } }))
      )), photo && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-photo-container" }, /* @__PURE__ */ import_react.default.createElement("img", { src: photo, alt: "photo", className: "pisell-camera-photo" })), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-camera-photo-img-action" }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-camera-photo-img-action-item",
          onClick: (e) => {
            e.stopPropagation();
            e.preventDefault();
            onRetakePhoto();
          }
        },
        (0, import_locales.getText)("pisell-upload-camera-retake")
      ), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-camera-photo-img-action-item",
          onClick: (e) => {
            e.stopPropagation();
            e.preventDefault();
            onUsePhoto();
          }
        },
        (0, import_locales.getText)("pisell-upload-camera-use")
      ))))
    )
  );
});
var pisellCamera_default = PisellCamera;
