import { getText } from "../../locales/index.js";
import IconFont from "../iconfont/index.js";
import PisellModal from "../pisellModal/index.js";
import React, { forwardRef, useImperativeHandle, useRef, useState } from "react";
import { Camera } from "react-camera-pro";
import "./index.less";
//#region src/components/pisellCamera/index.tsx
const PisellCamera = forwardRef((props, ref) => {
	const cameraRef = useRef(null);
	const [open, setOpen] = useState(false);
	const [photo, setPhoto] = useState("");
	const [cameraAvailable, setCameraAvailable] = useState(null);
	const uploadFileRef = useRef(null);
	const base64ToFile = (base64String, filename) => {
		var _arr$0$match;
		const arr = base64String.split(",");
		const mime = ((_arr$0$match = arr[0].match(/:(.*?);/)) === null || _arr$0$match === void 0 ? void 0 : _arr$0$match[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 = () => {
		if (!cameraRef.current || !cameraRef.current.takePhoto) return;
		try {
			var _cameraRef$current, _cameraRef$current$ta;
			const photo = (_cameraRef$current = cameraRef.current) === null || _cameraRef$current === void 0 || (_cameraRef$current$ta = _cameraRef$current.takePhoto) === null || _cameraRef$current$ta === void 0 ? void 0 : _cameraRef$current$ta.call(_cameraRef$current);
			const timestamp = Date.now();
			const filename = `photo-${timestamp}.jpg`;
			setPhoto(photo);
			const file = base64ToFile(photo, filename);
			const uploadFile = {
				uid: timestamp.toString(),
				name: filename,
				status: "uploading",
				url: photo,
				originFileObj: file,
				thumbUrl: photo,
				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 _props$onChange;
		props === null || props === void 0 || (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, uploadFileRef.current);
		onClose();
	};
	useImperativeHandle(ref, () => {
		return {
			open: onOpen,
			takePhoto,
			isCameraAvailable: () => cameraAvailable
		};
	});
	console.log("cameraAvailable", cameraAvailable);
	return /* @__PURE__ */ React.createElement(PisellModal, {
		open,
		onCancel: (e) => {
			e.stopPropagation();
			e.preventDefault();
			onClose();
		},
		header: false,
		footer: null,
		destroyOnClose: true,
		bodyStyle: { padding: 0 },
		width: "90vw",
		className: "pisell-camera-modal"
	}, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-camera-container",
		onClick: (e) => {
			e.stopPropagation();
			e.preventDefault();
		}
	}, cameraAvailable === null ? /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-loading" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-loading-text" }, getText("pisell-camera-checking"))) : cameraAvailable === false ? /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-no-camera" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-no-camera-icon" }, "📷"), /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-no-camera-text" }, getText("pisell-camera-no-camera-accessible")), /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-no-camera-desc" }, getText("pisell-camera-no-camera-desc"))) : null, /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Camera, {
		ref: cameraRef,
		facingMode: "environment",
		errorMessages: {},
		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__ */ React.createElement("div", { className: "pisell-camera-action" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-camera-action-item-wrap",
		onClick: (e) => {
			e.stopPropagation();
			e.preventDefault();
			takePhoto();
		}
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-action-item" }, /* @__PURE__ */ React.createElement(IconFont, {
		type: "pisell2-camera-01",
		style: { fontSize: 32 }
	})))), photo && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-photo-container" }, /* @__PURE__ */ React.createElement("img", {
		src: photo,
		alt: "photo",
		className: "pisell-camera-photo"
	})), /* @__PURE__ */ React.createElement("div", { className: "pisell-camera-photo-img-action" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-camera-photo-img-action-item",
		onClick: (e) => {
			e.stopPropagation();
			e.preventDefault();
			onRetakePhoto();
		}
	}, getText("pisell-upload-camera-retake")), /* @__PURE__ */ React.createElement("div", {
		className: "pisell-camera-photo-img-action-item",
		onClick: (e) => {
			e.stopPropagation();
			e.preventDefault();
			onUsePhoto();
		}
	}, getText("pisell-upload-camera-use")))))));
});
//#endregion
export { PisellCamera as default };
