/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, ref, computed, onMounted } from '@vue/composition-api';
import generateBase64Url from '../_common/js/watermark/generateBase64Url.js';
import randomMovingStyle from '../_common/js/watermark/randomMovingStyle.js';
import injectStyle from '../_common/js/utils/injectStyle.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useContent } from '../hooks/tnode.js';
import { useMutationObserver } from './hooks.js';
import props from './props.js';
import '../config-provider/useConfig.js';
import '../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../hooks/render-tnode.js';
import 'lodash/isString';
import 'lodash/isObject';
import '@babel/runtime/helpers/objectWithoutProperties';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Watermark = defineComponent({
  name: "TWatermark",
  props: props,
  setup: function setup(props2) {
    var backgroundImage = ref("");
    var watermarkRef = ref();
    var watermarkContentRef = ref();
    var parent = ref();
    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 () {
      var _props2$offset;

      return ((_props2$offset = props2.offset) === null || _props2$offset === void 0 ? void 0 : _props2$offset[0]) || gapX.value / 2;
    });
    var offsetTop = computed(function () {
      var _props2$offset2;

      return ((_props2$offset2 = props2.offset) === null || _props2$offset2 === void 0 ? void 0 : _props2$offset2[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
    };
    onMounted(function () {
      var _watermarkRef$value;

      generateBase64Url(bgImageOptions, function (base64Url) {
        backgroundImage.value = base64Url;
      });
      parent.value = (_watermarkRef$value = watermarkRef.value) === null || _watermarkRef$value === void 0 ? void 0 : _watermarkRef$value.parentElement;
      var keyframesStyle = randomMovingStyle();
      injectStyle(keyframesStyle);
      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
      });
    });
    return {
      gapX: gapX,
      gapY: gapY,
      backgroundRepeat: backgroundRepeat,
      backgroundImage: backgroundImage,
      watermarkRef: watermarkRef,
      watermarkContentRef: watermarkContentRef,
      bgImageOptions: bgImageOptions
    };
  },
  watch: {
    watermarkContent: function watermarkContent(content) {
      var _this = this;

      generateBase64Url(_objectSpread(_objectSpread({}, this.bgImageOptions), {}, {
        watermarkContent: content
      }), function (base64Url) {
        _this.backgroundImage = base64Url;
      });
    }
  },
  render: function render() {
    var h = arguments[0];
    var COMPONENT_NAME = usePrefixClass("watermark");
    var renderContent = useContent();
    return h("div", {
      "style": {
        position: "relative",
        overflow: "hidden",
        width: "100%"
      },
      "class": COMPONENT_NAME.value,
      "ref": "watermarkRef"
    }, [renderContent("default", "content"), h("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"
      }
    })]);
  }
});

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