var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
const Watermark = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("water-mark", prefixCls);
  const {
    width,
    height,
    zIndex,
    content,
    image,
    rotate,
    fullPage,
    gapX,
    gapY,
    textColor,
    opacity,
    children
  } = props;
  const [imageBase64, setImageBase64] = (0, import_react.useState)("");
  const svgElRef = (0, import_react.useRef)();
  const [watermarkUrl, setWatermarkUrl] = (0, import_react.useState)("");
  const renderWatermark = () => {
    const rotateStyle = {
      transformOrigin: "center",
      transform: `rotate(${rotate}deg)`
    };
    const svgInner = () => {
      if (image && !children) {
        return /* @__PURE__ */ import_react.default.createElement("image", {
          href: imageBase64,
          xlinkHref: imageBase64,
          x: "0",
          y: "0",
          width,
          height,
          style: rotateStyle
        });
      }
      return /* @__PURE__ */ import_react.default.createElement("foreignObject", {
        x: "0",
        y: "0",
        width,
        height
      }, /* @__PURE__ */ import_react.default.createElement("div", {
        // @ts-ignore
        xmlns: "http://www.w3.org/1999/xhtml",
        style: rotateStyle
      }, children || /* @__PURE__ */ import_react.default.createElement("span", {
        style: {
          color: textColor
        }
      }, content)));
    };
    const svgWidth = width + gapX;
    const svgHeight = height + gapY;
    return /* @__PURE__ */ import_react.default.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);
  };
  (0, import_react.useEffect)(() => {
    if (svgElRef.current) {
      if (watermarkUrl) {
        URL.revokeObjectURL(watermarkUrl);
      }
      setWatermarkUrl(makeSvgToBlobUrl(svgElRef.current.innerHTML));
    }
  }, [imageBase64, gapX, gapY, content, textColor, width, height, rotate]);
  (0, import_react.useEffect)(() => {
    if (image)
      makeImageToBase64(image);
  }, [image]);
  (0, import_react.useEffect)(() => {
    return () => {
      if (watermarkUrl) {
        URL.revokeObjectURL(watermarkUrl);
      }
    };
  }, []);
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem({
      full: fullPage
    })),
    style: {
      zIndex,
      backgroundImage: `url(${watermarkUrl})`,
      backgroundSize: `${gapX + width}px ${gapY + height}px `
    }
  }, /* @__PURE__ */ import_react.default.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;
