"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = showCropper;
var _react = _interopRequireWildcard(require("react"));
var _client = require("react-dom/client");
var _wauModal = _interopRequireDefault(require("../wau-modal"));
var _cropper = _interopRequireDefault(require("./cropper"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const CropperModal = props => {
  const {
    visible,
    open = visible,
    onClose,
    afterClose,
    width = 800,
    resultType,
    onCropperDone,
    title = '裁剪图片',
    ...cropperProps
  } = props;
  const cropperInstanceRef = (0, _react.useRef)();
  const handleModalOk = (0, _react.useCallback)(() => {
    if (cropperInstanceRef.current) {
      if (resultType === 'base64') {
        const url = cropperInstanceRef.current.getCroppedCanvas().toDataURL();
        onCropperDone === null || onCropperDone === void 0 ? void 0 : onCropperDone(url);
        onClose === null || onClose === void 0 ? void 0 : onClose();
      } else {
        cropperInstanceRef.current.getCroppedCanvas().toBlob(blob => {
          onCropperDone === null || onCropperDone === void 0 ? void 0 : onCropperDone(blob);
          onClose === null || onClose === void 0 ? void 0 : onClose();
        });
      }
    }
  }, [onClose, onCropperDone, resultType]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauModal.default, {
    title: title,
    open: open,
    width: width,
    onCancel: close,
    afterClose: afterClose,
    okText: "\u88C1\u526A",
    cancelText: "\u53D6\u6D88",
    onOk: handleModalOk,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_cropper.default, {
      ...cropperProps,
      ref: cropperInstanceRef,
      toolbar: {
        extra: false
      }
    })
  });
};
function showCropper(option) {
  const div = document.createElement('div');
  document.body.appendChild(div);
  const root = (0, _client.createRoot)(div);
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
  let currentConfig = {
    ...option,
    onClose: close,
    open: true
  };
  function destroy() {
    root.unmount();
    document.body.removeChild(div);
  }
  function close() {
    currentConfig = {
      ...currentConfig,
      open: false,
      afterClose: () => {
        if (currentConfig.onClose) {
          currentConfig.onClose();
        }
        destroy();
      }
    };
    root.render( /*#__PURE__*/(0, _jsxRuntime.jsx)(CropperModal, {
      ...currentConfig
    }));
  }
  root.render( /*#__PURE__*/(0, _jsxRuntime.jsx)(CropperModal, {
    ...currentConfig
  }));
}