"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const dom = require("../../ui/src/dom.js");
const util = require("./util.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const VxeWatermarkComponent = comp.defineVxeComponent({
  name: "VxeWatermark",
  props: {
    width: String,
    height: String,
    imageUrl: String,
    rotate: {
      type: [Number, String],
      default: () => config.getConfig().watermark.rotate
    },
    gap: {
      type: [Array, Number, String],
      default: () => XEUtils.clone(config.getConfig().watermark.gap, true)
    },
    content: [String, Array],
    font: Object,
    offset: Object,
    zIndex: [String, Number]
  },
  emits: [],
  setup(props, context) {
    const { emit } = context;
    const xID = XEUtils.uniqueId();
    const refElem = vue.ref();
    const reactData = vue.reactive({
      markUrl: ""
    });
    const refMaps = {
      refElem
    };
    const computeFontOpts = vue.computed(() => {
      return XEUtils.assign({}, XEUtils.clone(config.getConfig().watermark.font, true), props.font);
    });
    const computeWrapperStyle = vue.computed(() => {
      const { width, height, zIndex } = props;
      const { markUrl } = reactData;
      const stys = {};
      if (width) {
        stys.width = dom.toCssUnit(width);
      }
      if (height) {
        stys.height = dom.toCssUnit(height);
      }
      if (markUrl) {
        stys.backgroundImage = `url(${markUrl})`;
      }
      if (zIndex) {
        stys.zIndex = zIndex;
      }
      return stys;
    });
    const computeMaps = {};
    const $xeWatermark = {
      xID,
      props,
      context,
      reactData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $watermark: $xeWatermark }, params));
    };
    const collapsePaneMethods = {
      dispatchEvent
    };
    const updateMarkStyle = () => {
      const { content, gap, rotate, offset } = props;
      const el = refElem.value;
      const fontOpts = computeFontOpts.value;
      if (el) {
        if (content) {
          util.getContentUrl(content, getComputedStyle(el).fontSize, {
            font: fontOpts,
            rotate,
            gap,
            offset
          }).then((url) => {
            reactData.markUrl = url;
          });
        }
      }
    };
    const collapsePanePrivateMethods = {};
    Object.assign($xeWatermark, collapsePaneMethods, collapsePanePrivateMethods);
    const renderVN = () => {
      const wrapperStyle = computeWrapperStyle.value;
      return vue.h("div", {
        ref: refElem,
        class: "vxe-watermark",
        style: wrapperStyle
      });
    };
    vue.watch(() => props.imageUrl, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.content, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.gap, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.rotate, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.width, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.height, () => {
      updateMarkStyle();
    });
    vue.watch(() => props.font, () => {
      updateMarkStyle();
    });
    vue.onMounted(() => {
      updateMarkStyle();
      event.globalEvents.on($xeWatermark, "resize", XEUtils.throttle(() => {
        updateMarkStyle();
      }, 300, { trailing: true, leading: true }));
    });
    vue.onUnmounted(() => {
      event.globalEvents.off($xeWatermark, "resize");
    });
    $xeWatermark.renderVN = renderVN;
    return $xeWatermark;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeWatermarkComponent;
