/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { defineComponent, ref, reactive, computed, onMounted, watch, createVNode } from 'vue';
import generateBase64Url from '../_common/js/watermark/generateBase64Url.mjs';
import randomMovingStyle from '../_common/js/watermark/randomMovingStyle.mjs';
import injectStyle from '../_common/js/utils/injectStyle.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { useContent } from '../hooks/tnode.mjs';
import { useMutationObserver } from './hooks.mjs';
import props from './props.mjs';
import '../_chunks/dep-eb20e23f.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-6b22dd27.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../_chunks/dep-b6a73f93.mjs';

var _Watermark = defineComponent({
  name: "TWatermark",
  props: props,
  setup: function setup(props2) {
    var backgroundImage = ref("");
    var watermarkRef = ref();
    var watermarkContentRef = ref();
    var parent = ref();
    var offset = reactive(props2.offset || []);
    var gapX = computed(function () {
      return props2.movable ? 0 : props2.x;
    });
    var gapY = computed(function () {
      return props2.movable ? 0 : props2.y;
    });
    var rotate = computed(function () {
      return props2.movable ? 0 : props2.rotate;
    });
    var backgroundRepeat = computed(function () {
      if (props2.movable) return "no-repeat";
      return props2.isRepeat ? "repeat" : "no-repeat";
    });
    var offsetLeft = computed(function () {
      return offset[0] || gapX.value / 2;
    });
    var offsetTop = computed(function () {
      return offset[1] || gapY.value / 2;
    });
    var bgImageOptions = {
      width: props2.width,
      height: props2.height,
      rotate: rotate.value,
      lineSpace: props2.lineSpace,
      alpha: props2.alpha,
      gapX: gapX.value,
      gapY: gapY.value,
      watermarkContent: props2.watermarkContent,
      offsetLeft: offsetLeft.value,
      offsetTop: offsetTop.value
    };
    var injectWaterMark = function injectWaterMark() {
      generateBase64Url(bgImageOptions, function (base64Url) {
        backgroundImage.value = base64Url;
      });
      var keyframesStyle = randomMovingStyle();
      injectStyle(keyframesStyle);
    };
    onMounted(function () {
      injectWaterMark();
      parent.value = watermarkRef.value.parentElement;
      useMutationObserver(parent.value, function (mutations) {
        if (props2.removable) return;
        mutations.forEach(function (mutation) {
          if (mutation.type === "childList") {
            var removeNodes = mutation.removedNodes;
            removeNodes.forEach(function (node) {
              var element = node;
              if (element === watermarkRef.value) parent.value.appendChild(element);
              if (element === watermarkContentRef.value) watermarkRef.value.appendChild(element);
            });
          }
        });
      }, {
        attributes: true,
        childList: true,
        characterData: true,
        subtree: true
      });
    });
    watch(function () {
      return props2.watermarkContent;
    }, injectWaterMark, {
      deep: true
    });
    return {
      gapX: gapX,
      gapY: gapY,
      backgroundRepeat: backgroundRepeat,
      backgroundImage: backgroundImage,
      watermarkRef: watermarkRef,
      watermarkContentRef: watermarkContentRef,
      bgImageOptions: bgImageOptions
    };
  },
  render: function render() {
    var COMPONENT_NAME = usePrefixClass("watermark");
    var renderContent = useContent();
    return createVNode("div", {
      "style": {
        position: "relative",
        overflow: "hidden",
        width: "100%"
      },
      "class": COMPONENT_NAME.value,
      "ref": "watermarkRef"
    }, [renderContent("default", "content"), createVNode("div", {
      "ref": "watermarkContentRef",
      "style": {
        zIndex: this.zIndex,
        position: "absolute",
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        width: "100%",
        height: "100%",
        backgroundSize: "".concat(this.gapX + this.width, "px"),
        pointerEvents: "none",
        backgroundRepeat: this.backgroundRepeat,
        backgroundImage: "url('".concat(this.backgroundImage, "')"),
        animation: this.movable ? "watermark infinite ".concat(this.moveInterval * 4 / 60, "s") : "none"
      }
    }, null)]);
  }
});

export { _Watermark as default };
//# sourceMappingURL=watermark.mjs.map
