import { defineComponent, useAttrs, ref, computed, watch, watchEffect, resolveComponent, createElementBlock, openBlock, Fragment, createVNode, createElementVNode, withCtx, normalizeClass, unref, createBlock, createCommentVNode, mergeProps, normalizeStyle, renderSlot, withModifiers, toDisplayString } from 'vue';
import '../../../utils/index.mjs';
import { cropperModalEmits, cropperModalProps } from './cropper-image.mjs';
import './cropper-image.vue.mjs';
import '../../image/index.mjs';
import '../../watermark/index.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';
import { watermarkProps } from '../../watermark/src/watermark.mjs';
import { imageProps } from '../../image/src/image.mjs';
import { dataURLtoBlob } from '../../../utils/file.mjs';
import _sfc_main$1 from './cropper-image.vue2.mjs';

"use strict";
const _hoisted_1 = ["src"];
const _hoisted_2 = { style: { "display": "none" } };
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlCropperModal" },
  __name: "cropper-modal",
  props: cropperModalProps,
  emits: cropperModalEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace("cropper-modal");
    const attrs = useAttrs();
    const src = ref(props.avatar || "");
    const previewSource = ref("");
    const open = ref(false);
    const loading = ref(props.loading);
    const uploadLoading = ref(false);
    const imageVisible = ref(false);
    const isHover = ref(false);
    const cropper = ref();
    let scaleX = 1;
    let scaleY = 1;
    const sizeStyle = computed(() => {
      if (typeof props.avatarSize === "number") {
        return `${props.avatarSize}px`;
      } else if (typeof props.avatarSize === "string") {
        return props.avatarSize;
      } else return void 0;
    });
    const getImgBindValue = computed(() => {
      const imagePropKeys = Object.keys({ ...imageProps, ...watermarkProps });
      return Object.fromEntries(
        Object.entries(attrs).filter(([key]) => imagePropKeys.includes(key))
      );
    });
    const filteredAttrs = computed(() => {
      const watermarkKeys = Object.keys(watermarkProps);
      return Object.fromEntries(
        Object.entries(attrs).filter(([key]) => !watermarkKeys.includes(key))
      );
    });
    const handleBeforeUpload = (file) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      uploadLoading.value = true;
      src.value = "";
      previewSource.value = "";
      reader.onload = function(e) {
        src.value = e.target?.result ?? "";
        uploadLoading.value = false;
      };
      reader.onerror = () => {
        uploadLoading.value = false;
      };
      return false;
    };
    const handleOpen = () => {
      if (props.disabled) return;
      open.value = true;
    };
    const handleCropend = ({ imgBase64 }) => {
      previewSource.value = imgBase64;
    };
    const handleReady = (cropperInstance) => {
      cropper.value = cropperInstance;
    };
    const handlerToolbar = (event, arg) => {
      if (event === "scaleX") {
        scaleX = arg = scaleX === -1 ? 1 : -1;
      }
      if (event === "scaleY") {
        scaleY = arg = scaleY === -1 ? 1 : -1;
      }
      cropper?.value?.[event]?.(arg);
    };
    const handleOk = async () => {
      const blob = dataURLtoBlob(previewSource.value);
      emits("confirm", { blob, previewSource: previewSource.value });
      emits("update:avatar", previewSource.value);
      src.value = previewSource.value;
      open.value = false;
    };
    const handleCancel = async () => {
      await cleanValue();
      open.value = false;
    };
    const handleRemoveImage = () => {
      src.value = "";
      previewSource.value = "";
      emits("update:avatar", "");
    };
    const mouseenter = () => {
      isHover.value = true;
    };
    const mouseleave = () => {
      isHover.value = false;
    };
    const cleanValue = () => {
      src.value = "";
      previewSource.value = "";
    };
    watch(open, (val) => {
      if (val) {
        src.value = props.avatar;
        previewSource.value = props.avatar;
      }
    });
    watchEffect(() => {
      loading.value = props.loading;
    });
    __expose({
      cleanValue
    });
    return (_ctx, _cache) => {
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_ll_button = resolveComponent("ll-button");
      const _component_ll_tooltip = resolveComponent("ll-tooltip");
      const _component_a_upload = resolveComponent("a-upload");
      const _component_ll_space = resolveComponent("ll-space");
      const _component_ll_image = resolveComponent("ll-image");
      const _component_a_spin = resolveComponent("a-spin");
      const _component_ll_new_modal = resolveComponent("ll-new-modal");
      const _component_a_image = resolveComponent("a-image");
      const _component_a_image_preview_group = resolveComponent("a-image-preview-group");
      return openBlock(), createElementBlock(
        Fragment,
        null,
        [
          createVNode(_component_ll_new_modal, {
            open: open.value,
            title: props.title,
            width: 800,
            keyboard: false,
            "mask-closable": false,
            "ok-text": "\u786E\u5B9A",
            loading: uploadLoading.value,
            onCancel: handleCancel,
            onOk: handleOk
          }, {
            default: withCtx(() => [
              createElementVNode(
                "div",
                {
                  class: normalizeClass(unref(bem).b())
                },
                [
                  createElementVNode(
                    "div",
                    {
                      class: normalizeClass(unref(bem).e("left"))
                    },
                    [
                      createElementVNode(
                        "div",
                        {
                          class: normalizeClass(unref(bem).e("cropper"))
                        },
                        [
                          src.value && open.value ? (openBlock(), createBlock(_sfc_main$1, {
                            key: 0,
                            src: src.value,
                            height: "300px",
                            circled: _ctx.circled,
                            loading: uploadLoading.value,
                            onCropend: handleCropend,
                            onOnReady: handleReady
                          }, null, 8, ["src", "circled", "loading"])) : createCommentVNode("v-if", true)
                        ],
                        2
                        /* CLASS */
                      ),
                      createElementVNode(
                        "div",
                        {
                          class: normalizeClass(unref(bem).e("toolbar"))
                        },
                        [
                          createVNode(_component_ll_space, null, {
                            default: withCtx(() => [
                              createVNode(_component_a_upload, {
                                "file-list": [],
                                accept: "image/*",
                                "before-upload": handleBeforeUpload
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_tooltip, {
                                    title: "\u9009\u62E9\u56FE\u7247",
                                    placement: "bottom"
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_button, {
                                        type: "primary",
                                        size: "small",
                                        disabled: uploadLoading.value,
                                        onClick: _cache[0] || (_cache[0] = ($event) => handlerToolbar("reset"))
                                      }, {
                                        default: withCtx(() => [
                                          createVNode(_component_ll_icon, { "icon-name": "icon-upload" })
                                        ]),
                                        _: 1
                                        /* STABLE */
                                      }, 8, ["disabled"])
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  })
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u91CD\u7F6E",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[1] || (_cache[1] = ($event) => handlerToolbar("reset"))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-refresh" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u9006\u65F6\u9488\u65CB\u8F6C",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[2] || (_cache[2] = ($event) => handlerToolbar("rotate", -45))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-counter-clockwise" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u987A\u65F6\u9488\u65CB\u8F6C",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[3] || (_cache[3] = ($event) => handlerToolbar("rotate", 45))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-clockwise-rotation" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u6C34\u5E73\u7FFB\u8F6C",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[4] || (_cache[4] = ($event) => handlerToolbar("scaleX"))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-flip-horizontal" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u5782\u76F4\u7FFB\u8F6C",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[5] || (_cache[5] = ($event) => handlerToolbar("scaleY"))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-flip-vertical" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u653E\u5927",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[6] || (_cache[6] = ($event) => handlerToolbar("zoom", 0.1))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-amplify1" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              }),
                              createVNode(_component_ll_tooltip, {
                                tip: "\u7F29\u5C0F",
                                placement: "bottom"
                              }, {
                                default: withCtx(() => [
                                  createVNode(_component_ll_button, {
                                    type: "primary",
                                    size: "small",
                                    disabled: !src.value || uploadLoading.value,
                                    onClick: _cache[7] || (_cache[7] = ($event) => handlerToolbar("zoom", -0.1))
                                  }, {
                                    default: withCtx(() => [
                                      createVNode(_component_ll_icon, { "icon-name": "icon-scale" })
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  }, 8, ["disabled"])
                                ]),
                                _: 1
                                /* STABLE */
                              })
                            ]),
                            _: 1
                            /* STABLE */
                          })
                        ],
                        2
                        /* CLASS */
                      )
                    ],
                    2
                    /* CLASS */
                  ),
                  createElementVNode(
                    "div",
                    {
                      class: normalizeClass(unref(bem).e("right"))
                    },
                    [
                      createVNode(_component_a_spin, { spinning: uploadLoading.value }, {
                        default: withCtx(() => [
                          createElementVNode(
                            "div",
                            {
                              class: normalizeClass(unref(bem).e("preview"))
                            },
                            [
                              previewSource.value ? (openBlock(), createElementBlock("img", {
                                key: 0,
                                src: previewSource.value,
                                alt: "\u9884\u89C8"
                              }, null, 8, _hoisted_1)) : createCommentVNode("v-if", true)
                            ],
                            2
                            /* CLASS */
                          ),
                          previewSource.value ? (openBlock(), createElementBlock(
                            "div",
                            {
                              key: 0,
                              class: normalizeClass(unref(bem).e("group"))
                            },
                            [
                              createVNode(_component_ll_image, {
                                preview: false,
                                src: previewSource.value,
                                width: 32,
                                height: 32
                              }, null, 8, ["src"]),
                              createVNode(_component_ll_image, {
                                preview: false,
                                src: previewSource.value,
                                width: 48,
                                height: 48
                              }, null, 8, ["src"]),
                              createVNode(_component_ll_image, {
                                preview: false,
                                src: previewSource.value,
                                width: 64,
                                height: 64
                              }, null, 8, ["src"]),
                              createVNode(_component_ll_image, {
                                preview: false,
                                src: previewSource.value,
                                width: 80,
                                height: 80
                              }, null, 8, ["src"])
                            ],
                            2
                            /* CLASS */
                          )) : createCommentVNode("v-if", true)
                        ]),
                        _: 1
                        /* STABLE */
                      }, 8, ["spinning"])
                    ],
                    2
                    /* CLASS */
                  )
                ],
                2
                /* CLASS */
              )
            ]),
            _: 1
            /* STABLE */
          }, 8, ["open", "title", "loading"]),
          createElementVNode(
            "div",
            mergeProps({ style: { display: "inline-block" } }, filteredAttrs.value),
            [
              createElementVNode(
                "div",
                {
                  class: normalizeClass([
                    unref(bem).e("cropper-avatar"),
                    props.disabled ? unref(bem).is("disabled") : ""
                  ]),
                  style: normalizeStyle({
                    cursor: props.disabled ? "not-allowed" : "pointer",
                    display: "inline-flex",
                    borderRadius: _ctx.borderRadius,
                    border: `1px var(--color-border) ${_ctx.borderType}`
                  }),
                  onClick: handleOpen,
                  onMouseenter: mouseenter,
                  onMouseleave: mouseleave
                },
                [
                  createVNode(_component_a_spin, {
                    spinning: loading.value,
                    style: { "display": "inline-block" }
                  }, {
                    default: withCtx(() => [
                      renderSlot(_ctx.$slots, "default", { src: _ctx.avatar }, () => [
                        _ctx.avatar ? (openBlock(), createBlock(_component_ll_image, mergeProps({ key: 0 }, getImgBindValue.value, {
                          src: _ctx.avatar,
                          width: _ctx.avatarSize,
                          height: _ctx.avatarSize,
                          "is-show-border": false,
                          "border-radius": _ctx.borderRadius,
                          preview: { visible: false }
                        }), {
                          previewMaskContent: withCtx(() => [
                            _ctx.maskMode === "default" ? (openBlock(), createElementBlock(
                              "div",
                              {
                                key: 0,
                                class: normalizeClass(unref(bem).m("preview-mask-default"))
                              },
                              [
                                createVNode(_component_ll_icon, {
                                  "icon-name": `icon-pwd-look`,
                                  class: normalizeClass(unref(bem).e("image-icon")),
                                  onClick: _cache[8] || (_cache[8] = withModifiers(($event) => imageVisible.value = true, ["stop"]))
                                }, null, 8, ["class"]),
                                createVNode(_component_ll_icon, {
                                  "icon-name": `icon-delete`,
                                  class: normalizeClass(unref(bem).e("image-icon")),
                                  onClick: withModifiers(handleRemoveImage, ["stop"])
                                }, null, 8, ["class"])
                              ],
                              2
                              /* CLASS */
                            )) : (openBlock(), createElementBlock(
                              Fragment,
                              { key: 1 },
                              [
                                createCommentVNode(" \u6E10\u5165\u5F0F "),
                                createElementVNode(
                                  "div",
                                  {
                                    class: normalizeClass(unref(bem).m("preview-mask-fade-in")),
                                    style: normalizeStyle({ height: isHover.value ? _ctx.maskHeight : "0" })
                                  },
                                  [
                                    createElementVNode(
                                      "div",
                                      {
                                        class: normalizeClass(unref(bem).m("mask-text")),
                                        style: normalizeStyle({ fontSize: _ctx.maskTextSize })
                                      },
                                      toDisplayString(_ctx.maskText),
                                      7
                                      /* TEXT, CLASS, STYLE */
                                    )
                                  ],
                                  6
                                  /* CLASS, STYLE */
                                )
                              ],
                              2112
                              /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */
                            ))
                          ]),
                          _: 1
                          /* STABLE */
                        }, 16, ["src", "width", "height", "border-radius"])) : (openBlock(), createElementBlock(
                          "div",
                          {
                            key: 1,
                            class: normalizeClass(unref(bem).e("cropper-upload")),
                            style: normalizeStyle({ width: sizeStyle.value, height: sizeStyle.value })
                          },
                          [
                            createVNode(_component_ll_icon, {
                              "icon-name": _ctx.uploadIcon || "icon-add",
                              size: _ctx.uploadIconSize
                            }, null, 8, ["icon-name", "size"]),
                            createCommentVNode(" \u4E0A\u4F20\u6807\u9898 "),
                            _ctx.uploadTitle ? (openBlock(), createElementBlock(
                              "div",
                              {
                                key: 0,
                                class: normalizeClass(unref(bem).m("upload-title"))
                              },
                              toDisplayString(_ctx.uploadTitle),
                              3
                              /* TEXT, CLASS */
                            )) : createCommentVNode("v-if", true),
                            createCommentVNode(" \u4E0A\u4F20\u63D0\u793A "),
                            _ctx.uploadTip ? (openBlock(), createElementBlock(
                              "div",
                              {
                                key: 1,
                                class: normalizeClass(unref(bem).m("upload-tip"))
                              },
                              toDisplayString(_ctx.uploadTip),
                              3
                              /* TEXT, CLASS */
                            )) : createCommentVNode("v-if", true)
                          ],
                          6
                          /* CLASS, STYLE */
                        ))
                      ])
                    ]),
                    _: 3
                    /* FORWARDED */
                  }, 8, ["spinning"]),
                  createElementVNode("div", _hoisted_2, [
                    createVNode(_component_a_image_preview_group, {
                      preview: {
                        visible: imageVisible.value,
                        onVisibleChange: (vis) => imageVisible.value = vis
                      }
                    }, {
                      default: withCtx(() => [
                        createVNode(_component_a_image, { src: _ctx.avatar }, null, 8, ["src"])
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["preview"])
                  ])
                ],
                38
                /* CLASS, STYLE, NEED_HYDRATION */
              )
            ],
            16
            /* FULL_PROPS */
          )
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=cropper-modal.vue2.mjs.map
