"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "WaterMark", {
    enumerable: true,
    get: function() {
        return WaterMark;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _taro = require("@tarojs/taro");
var _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _index = require("../configprovider/index");
var _typings = require("../../utils/typings");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    content: '',
    fullPage: true,
    zIndex: 2000,
    gapX: 24,
    gapY: 48,
    width: 120,
    height: 64,
    startX: 0,
    startY: 0,
    image: '',
    imageWidth: 120,
    imageHeight: 64,
    rotate: -22,
    color: 'rgba(0,0,0,.15)',
    fontStyle: 'normal',
    fontWeight: 'normal',
    fontSize: 14
});
var WaterMark = function WaterMark(props) {
    var locale = (0, _index.useConfig)().locale;
    var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), content = _$_object_spread.content, fullPage = _$_object_spread.fullPage, zIndex = _$_object_spread.zIndex, className = _$_object_spread.className, gapX = _$_object_spread.gapX, gapY = _$_object_spread.gapY, startX = _$_object_spread.startX, startY = _$_object_spread.startY, width = _$_object_spread.width, height = _$_object_spread.height, image = _$_object_spread.image, imageWidth = _$_object_spread.imageWidth, imageHeight = _$_object_spread.imageHeight, rotate = _$_object_spread.rotate, color = _$_object_spread.color, fontStyle = _$_object_spread.fontStyle, fontFamily = _$_object_spread.fontFamily, fontWeight = _$_object_spread.fontWeight, fontSize = _$_object_spread.fontSize, style = _$_object_spread.style;
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(''), 2), base64Url = _useState[0], setBase64Url = _useState[1];
    var classPrefix = 'nut-watermark';
    var classes = (0, _classnames.default)(classPrefix, (0, _define_property._)({}, "".concat(classPrefix, "-full-page"), fullPage));
    var cls = (0, _classnames.default)(classes, className);
    (0, _react.useEffect)(function() {
        init();
    }, []);
    var init = function init() {
        var ratio = 1;
        (0, _taro.getSystemInfo)().then(function(res) {
            ratio = res.pixelRatio;
            var canvasWidth = "".concat((gapX + width) * ratio);
            var canvasHeight = "".concat((gapY + height) * ratio);
            var markWidth = width * ratio;
            var markHeight = height * ratio;
            var ctx;
            var canvas;
            if (process.env.TARO_ENV === 'h5') {
                canvas = document.createElement('canvas');
                ctx = canvas.getContext('2d');
                canvas.setAttribute('width', "".concat(canvasWidth, "px"));
                canvas.setAttribute('height', "".concat(canvasHeight, "px"));
            } else {
                canvas = (0, _taro.createOffscreenCanvas)({
                    type: '2d',
                    width: Number(canvasWidth),
                    height: Number(canvasHeight)
                });
                ctx = canvas.getContext('2d');
            }
            if (ctx) {
                if (image) {
                    ctx.translate(markWidth / 2, markHeight / 2);
                    ctx.rotate(Math.PI / 180 * Number(rotate));
                    var img;
                    if (process.env.TARO_ENV === 'h5') {
                        img = new Image();
                    } else {
                        img = canvas.createImage();
                    }
                    img.crossOrigin = 'anonymous';
                    img.referrerPolicy = 'no-referrer';
                    img.src = image;
                    img.onload = function() {
                        ctx.drawImage(img, -imageWidth * ratio / 2, -imageHeight * ratio / 2, imageWidth * ratio, imageHeight * ratio);
                        ctx.restore();
                        setBase64Url(canvas.toDataURL());
                    };
                } else if (content) {
                    ctx.textBaseline = 'middle'; // 设置或返回在绘制文本时使用的当前文本基线。(正中)
                    ctx.textAlign = 'center'; // 设置或返回文本内容的当前对齐方式。
                    // 文字绕中间旋转
                    ctx.translate(markWidth / 2, markHeight / 2);
                    ctx.rotate(Math.PI / 180 * Number(rotate));
                    var markSize = Number(fontSize) * ratio;
                    ctx.font = "".concat(fontStyle, " normal ").concat(fontWeight, " ").concat(markSize, "px/").concat(markHeight, "px ").concat(fontFamily);
                    ctx.fillStyle = color;
                    ctx.fillText(content, startX, startY); // 在画布上绘制"被填充的"文本。
                    ctx.restore(); // 返回之前保存过的路径状态和属性。
                    setBase64Url(canvas.toDataURL());
                }
            } else {
                throw new Error(locale.watermark.errorCanvasTips);
            }
        });
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: cls,
        style: (0, _object_spread._)({
            zIndex: zIndex,
            backgroundSize: "".concat(gapX + width, "px"),
            backgroundImage: "url('".concat(base64Url, "')")
        }, style)
    });
};
WaterMark.displayName = 'NutWaterMark';
