import React, { useContext, useEffect, useRef, useState } from "react";
import classnames from "classnames";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const Watermark = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("water-mark", prefixCls);
  const {
    width,
    height,
    zIndex,
    content,
    image,
    rotate,
    fullPage,
    gapX,
    gapY,
    textColor,
    opacity,
    children
  } = props;
  const [imageBase64, setImageBase64] = useState("");
  const svgElRef = useRef();
  const [watermarkUrl, setWatermarkUrl] = useState("");
  const renderWatermark = () => {
    const rotateStyle = {
      transformOrigin: "center",
      transform: `rotate(${rotate}deg)`
    };
    const svgInner = () => {
      if (image && !children) {
        return /* @__PURE__ */ React.createElement("image", {
          href: imageBase64,
          xlinkHref: imageBase64,
          x: "0",
          y: "0",
          width,
          height,
          style: rotateStyle
        });
      }
      return /* @__PURE__ */ React.createElement("foreignObject", {
        x: "0",
        y: "0",
        width,
        height
      }, /* @__PURE__ */ React.createElement("div", {
        // @ts-ignore
        xmlns: "http://www.w3.org/1999/xhtml",
        style: rotateStyle
      }, children || /* @__PURE__ */ React.createElement("span", {
        style: {
          color: textColor
        }
      }, content)));
    };
    const svgWidth = width + gapX;
    const svgHeight = height + gapY;
    return /* @__PURE__ */ React.createElement("svg", {
      viewBox: `0 0 ${svgWidth} ${svgHeight}`,
      width: svgWidth,
      height: svgHeight,
      xmlns: "http://www.w3.org/2000/svg",
      xmlnsXlink: "http://www.w3.org/1999/xlink",
      style: {
        padding: `0 ${gapX}px ${gapY}px 0`,
        opacity,
        display: "none"
      }
    }, svgInner());
  };
  const makeImageToBase64 = (url) => {
    const canvas = document.createElement("canvas");
    const imageEl = new Image();
    imageEl.crossOrigin = "anonymous";
    imageEl.referrerPolicy = "no-referrer";
    imageEl.onload = () => {
      canvas.width = imageEl.naturalWidth;
      canvas.height = imageEl.naturalHeight;
      const ctx = canvas.getContext("2d");
      ctx == null ? void 0 : ctx.drawImage(imageEl, 0, 0);
      setImageBase64(canvas.toDataURL());
    };
    imageEl.src = url;
  };
  const makeSvgToBlobUrl = (svgStr) => {
    const svgBlob = new Blob([svgStr], {
      type: "image/svg+xml"
    });
    return URL.createObjectURL(svgBlob);
  };
  useEffect(() => {
    if (svgElRef.current) {
      if (watermarkUrl) {
        URL.revokeObjectURL(watermarkUrl);
      }
      setWatermarkUrl(makeSvgToBlobUrl(svgElRef.current.innerHTML));
    }
  }, [imageBase64, gapX, gapY, content, textColor, width, height, rotate]);
  useEffect(() => {
    if (image)
      makeImageToBase64(image);
  }, [image]);
  useEffect(() => {
    return () => {
      if (watermarkUrl) {
        URL.revokeObjectURL(watermarkUrl);
      }
    };
  }, []);
  return /* @__PURE__ */ React.createElement("div", {
    className: classnames(bem({
      full: fullPage
    })),
    style: {
      zIndex,
      backgroundImage: `url(${watermarkUrl})`,
      backgroundSize: `${gapX + width}px ${gapY + height}px `
    }
  }, /* @__PURE__ */ React.createElement("div", {
    ref: svgElRef
  }, renderWatermark()));
};
Watermark.defaultProps = {
  width: 100,
  height: 100,
  zIndex: 100,
  rotate: -22,
  fullPage: false,
  gapX: 0,
  gapY: 0,
  textColor: "#dcdee0"
};
var stdin_default = Watermark;
export {
  stdin_default as default
};
