import { __awaiter as X, __rest as je } from "../../tslib/tslib.es6.mjs";
import { j as o } from "../../react/jsx-runtime.mjs";
import Ae from "../../antd/es/modal/index.mjs";
import Me from "../../antd/es/upload/index.mjs";
import { forwardRef as ve, useState as H, useRef as U, useCallback as D, useImperativeHandle as Pe, memo as ke, useMemo as Ee } from "react";
import $e from "../../antd/es/button/button.mjs";
import ie from "../../antd/es/slider/index.mjs";
import ze from "../../react-easy-crop/index.module.mjs";
import { compareVersions as Fe } from "../../compare-versions/lib/esm/compareVersions.mjs";
import Be from "../../antd/es/version/version.mjs";
const C = "img-crop", ae = 1, B = 0.1, Y = 0, xe = -180, Ce = 180, re = 1, be = 0.5, ye = 2, Z = 0.01, Ze = ve((n, m) => {
  const { cropperRef: b, zoomSlider: c, rotationSlider: u, aspectSlider: V, showReset: J, resetBtnText: K, modalImage: Q, aspect: A, minZoom: M, maxZoom: P, cropShape: ce, showGrid: le, cropperProps: ee } = n, [g, R] = H(ae), [d, y] = H(Y), [h, _] = H(A), te = g !== ae || d !== Y || h !== A, G = () => {
    R(ae), y(Y), _(A);
  }, [oe, ne] = H({ x: 0, y: 0 }), L = U({ width: 0, height: 0, x: 0, y: 0 }), k = D((I, W) => {
    L.current = W;
  }, []);
  Pe(m, () => ({
    rotation: d,
    cropPixelsRef: L,
    onReset: G
  }));
  const E = "[display:flex] [align-items:center] [width:60%] [margin-inline:auto]", w = "[display:flex] [align-items:center] [justify-content:center] [height:32px] [width:32px] [background:transparent] [border:0] [font-family:inherit] [font-size:18px] [cursor:pointer] disabled:[opacity:20%] disabled:[cursor:default]", p = "[flex:1]";
  return o.jsxs(o.Fragment, { children: [o.jsx(ze, Object.assign({}, ee, {
    ref: b,
    image: Q,
    crop: oe,
    //
    zoom: g,
    rotation: d,
    aspect: h,
    minZoom: M,
    maxZoom: P,
    zoomWithScroll: c,
    //
    cropShape: ce,
    showGrid: le,
    onCropChange: ne,
    onZoomChange: R,
    onRotationChange: y,
    onCropComplete: k,
    classes: {
      containerClassName: `${C}-container ![position:relative] [width:100%] [height:40vh] [&~section:first-of-type]:[margin-top:16px] [&~section:last-of-type]:[margin-bottom:16px]`,
      mediaClassName: `${C}-media`
    }
  })), c && o.jsxs("section", { className: `${C}-control ${C}-control-zoom ${E}`, children: [o.jsx("button", { className: w, onClick: () => R(g - B), disabled: g - B < M, children: "－" }), o.jsx(ie, { className: p, min: M, max: P, step: B, value: g, onChange: R }), o.jsx("button", { className: w, onClick: () => R(g + B), disabled: g + B > P, children: "＋" })] }), u && o.jsxs("section", { className: `${C}-control ${C}-control-rotation ${E}`, children: [o.jsx("button", { className: `${w} [font-size:16px]`, onClick: () => y(d - re), disabled: d === xe, children: "↺" }), o.jsx(ie, { className: p, min: xe, max: Ce, step: re, value: d, onChange: y }), o.jsx("button", { className: `${w} [font-size:16px]`, onClick: () => y(d + re), disabled: d === Ce, children: "↻" })] }), V && o.jsxs("section", { className: `${C}-control ${C}-control-aspect ${E}`, children: [o.jsx("button", { className: w, onClick: () => _(h - Z), disabled: h - Z < be, children: "↕️" }), o.jsx(ie, { className: p, min: be, max: ye, step: Z, value: h, onChange: _ }), o.jsx("button", { className: w, onClick: () => _(h + Z), disabled: h + Z > ye, children: "↔️" })] }), J && (c || u || V) && o.jsx($e, { className: "[bottom:20px] [position:absolute]", style: te ? {} : { opacity: 0.3, pointerEvents: "none" }, onClick: G, children: K })] });
});
var He = ke(Ze);
function Ue(n, m) {
  m === void 0 && (m = {});
  var b = m.insertAt;
  if (!(!n || typeof document > "u")) {
    var c = document.head || document.getElementsByTagName("head")[0], u = document.createElement("style");
    u.type = "text/css", b === "top" && c.firstChild ? c.insertBefore(u, c.firstChild) : c.appendChild(u), u.styleSheet ? u.styleSheet.cssText = n : u.appendChild(document.createTextNode(n));
  }
}
var De = ".visible{visibility:visible}.grid{display:grid}.\\!\\[position\\:relative\\]{position:relative!important}.\\[align-items\\:center\\]{align-items:center}.\\[background\\:transparent\\]{background:transparent}.\\[border\\:0\\]{border:0}.\\[bottom\\:20px\\]{bottom:20px}.\\[cursor\\:pointer\\]{cursor:pointer}.\\[display\\:flex\\]{display:flex}.\\[flex\\:1\\]{flex:1}.\\[font-family\\:inherit\\]{font-family:inherit}.\\[font-size\\:16px\\]{font-size:16px}.\\[font-size\\:18px\\]{font-size:18px}.\\[height\\:32px\\]{height:32px}.\\[height\\:40vh\\]{height:40vh}.\\[justify-content\\:center\\]{justify-content:center}.\\[margin-inline\\:auto\\]{margin-inline:auto}.\\[position\\:absolute\\]{position:absolute}.\\[width\\:100\\%\\]{width:100%}.\\[width\\:32px\\]{width:32px}.\\[width\\:60\\%\\]{width:60%}.disabled\\:\\[cursor\\:default\\]:disabled{cursor:default}.disabled\\:\\[opacity\\:20\\%\\]:disabled{opacity:20%}.\\[\\&\\~section\\:first-of-type\\]\\:\\[margin-top\\:16px\\]~section:first-of-type{margin-top:16px}.\\[\\&\\~section\\:last-of-type\\]\\:\\[margin-bottom\\:16px\\]~section:last-of-type{margin-bottom:16px}";
Ue(De, { insertAt: "top" });
const Ge = Fe(Be, "4.23.0") === -1 ? "visible" : "open", j = (n, m, b) => m in n ? (console.error(`\`${m}\` is deprecated, please use \`${b}\` instead`), n[m]) : n[b], ot = ve((n, m) => {
  const {
    quality: b = 0.4,
    fillColor: c = "white",
    // @ts-ignore
    zoomSlider: u = !0,
    // @ts-ignore
    rotationSlider: V = !1,
    aspectSlider: J = !1,
    showReset: K = !1,
    resetText: Q,
    aspect: A = 1,
    minZoom: M = 1,
    maxZoom: P = 3,
    // @ts-ignore
    cropShape: ce = "rect",
    // @ts-ignore
    showGrid: le = !1,
    cropperProps: ee,
    modalClassName: g,
    modalTitle: R,
    modalWidth: d,
    modalOk: y,
    modalCancel: h,
    onModalOk: _,
    onModalCancel: te,
    modalProps: G,
    beforeCrop: oe,
    children: ne
  } = n, L = j(n, "zoom", "zoomSlider") || !0, k = j(n, "rotate", "rotationSlider") || !1, E = j(n, "shape", "cropShape") || "rect", w = j(n, "grid", "showGrid") || !1;
  "onUploadFail" in n && console.error("`onUploadFail` is removed, because the only way it is called, is when the file is rejected by beforeUpload"), j(n, "modalMaskTransitionName", "modalProps.maskTransitionName"), j(n, "modalTransitionName", "modalProps.transitionName");
  const p = U({});
  p.current.onModalOk = _, p.current.onModalCancel = te, p.current.beforeCrop = oe;
  const I = U(null), W = D((t) => {
    var s;
    const i = document.createElement("canvas"), e = i.getContext("2d"), r = (((s = t == null ? void 0 : t.getRootNode) === null || s === void 0 ? void 0 : s.call(t)) || document).querySelector(`.${C}-media`), { width: x, height: a, x: N, y: $ } = I.current.cropPixelsRef.current;
    if (k && I.current.rotation !== Y) {
      const { naturalWidth: O, naturalHeight: T } = r, z = I.current.rotation * (Math.PI / 180), F = Math.abs(Math.sin(z)), S = Math.abs(Math.cos(z)), f = O * S + T * F, l = T * S + O * F;
      i.width = f, i.height = l, e.fillStyle = c, e.fillRect(0, 0, f, l);
      const fe = f / 2, ge = l / 2;
      e.translate(fe, ge), e.rotate(z), e.translate(-fe, -ge);
      const Oe = (f - O) / 2, Te = (l - T) / 2;
      e.drawImage(r, 0, 0, O, T, Oe, Te, O, T);
      const _e = e.getImageData(0, 0, f, l);
      i.width = x, i.height = a, e.putImageData(_e, -N, -$);
    } else
      i.width = x, i.height = a, e.fillStyle = c, e.fillRect(0, 0, x, a), e.drawImage(r, N, $, x, a, 0, 0, x, a);
    return i;
  }, [c, k]), [de, se] = H(""), pe = U(), me = U(), q = D((t, s, i, e) => X(void 0, void 0, void 0, function* () {
    const v = s;
    if (typeof t != "function") {
      i(v);
      return;
    }
    try {
      const r = yield t(s, [s]);
      i(r !== !0 && r || v);
    } catch (r) {
      e(r);
    }
  }), []), ue = D((t) => (s, i) => new Promise((e, v) => X(void 0, void 0, void 0, function* () {
    let r = s;
    if (typeof p.current.beforeCrop == "function")
      try {
        const a = yield p.current.beforeCrop(s, i);
        if (a === !1)
          return q(t, s, e, v);
        a !== !0 && (r = a || s);
      } catch {
        return q(t, s, e, v);
      }
    const x = new FileReader();
    x.addEventListener("load", () => {
      typeof x.result == "string" && se(x.result);
    }), x.readAsDataURL(r), pe.current = () => {
      var a, N;
      se(""), I.current.onReset(), e(Me.LIST_IGNORE), (N = (a = p.current).onModalCancel) === null || N === void 0 || N.call(a, e);
    }, me.current = (a) => X(void 0, void 0, void 0, function* () {
      se(""), I.current.onReset();
      const N = W(a.target), { type: $, name: O, uid: T } = r;
      N.toBlob((z) => X(void 0, void 0, void 0, function* () {
        const F = new File([z], O, { type: $ });
        Object.assign(F, { uid: T }), q(t, F, (S) => {
          var f, l;
          e(S), (l = (f = p.current).onModalOk) === null || l === void 0 || l.call(f, S);
        }, (S) => {
          var f, l;
          v(S), (l = (f = p.current).onModalOk) === null || l === void 0 || l.call(f, S);
        });
      }), $, b);
    });
  })), [W, b, q]), we = D((t) => {
    const s = Array.isArray(t) ? t[0] : t, i = s.props, { beforeUpload: e, accept: v } = i, r = je(i, ["beforeUpload", "accept"]);
    return Object.assign(Object.assign({}, s), { props: Object.assign(Object.assign({}, r), { accept: v || "image/*", beforeUpload: ue(e) }) });
  }, [ue]), Ne = Ee(() => {
    const t = {};
    return d !== void 0 && (t.width = d), y !== void 0 && (t.okText = y), h !== void 0 && (t.cancelText = h), t;
  }, [h, y, d]), Se = `${C}-modal${g ? ` ${g}` : ""}`, he = (typeof window > "u" ? "" : window.navigator.language) === "zh-CN", Re = R || (he ? "编辑图片" : "Edit image"), Ie = Q || (he ? "重置" : "Reset");
  return o.jsxs(o.Fragment, { children: [we(ne), de && o.jsx(Ae, Object.assign({}, G, Ne, { [Ge]: !0, title: Re, onCancel: pe.current, onOk: me.current, wrapClassName: Se, maskClosable: !1, destroyOnClose: !0, children: o.jsx(He, { ref: I, cropperRef: m, zoomSlider: L, rotationSlider: k, aspectSlider: J, showReset: K, resetBtnText: Ie, modalImage: de, aspect: A, minZoom: M, maxZoom: P, cropShape: E, showGrid: w, cropperProps: ee }) }))] });
});
export {
  ot as default
};
