function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import React, { Component } from 'react'; // @ts-ignore

import isEqual from "lodash/isEqual";

var WaterMark = /*#__PURE__*/function (_Component) {
  _inherits(WaterMark, _Component);

  var _super = _createSuper(WaterMark);

  // @ts-ignore
  function WaterMark(props) {
    var _this;

    _classCallCheck(this, WaterMark);

    _this = _super.call(this, props);
    _this.canvas = void 0;
    _this.ctx = void 0;

    if (typeof document === 'undefined') {
      _this.canvas = null;
      _this.ctx = null;
    } else {
      _this.canvas = document.createElement('canvas');
      _this.ctx = _this.canvas.getContext('2d');
    }

    return _this;
  } // @ts-ignore


  _createClass(WaterMark, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      return !isEqual(this.props, nextProps);
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props,
          _this$props = this.props,
          content = _this$props.content,
          _this$props$rotation = _this$props.rotation,
          rotation = _this$props$rotation === void 0 ? 0 : _this$props$rotation,
          _this$props$fontFamil = _this$props.fontFamily,
          fontFamily = _this$props$fontFamil === void 0 ? 'serif' : _this$props$fontFamil,
          _this$props$fontColor = _this$props.fontColor,
          fontColor = _this$props$fontColor === void 0 ? 'white' : _this$props$fontColor,
          children = _this$props.children;

      if (typeof document === 'undefined') {
        return /*#__PURE__*/React.createElement("div", props, children);
      }

      var _props$fontSize = props.fontSize,
          fontSize = _props$fontSize === void 0 ? 16 : _props$fontSize,
          _props$lineHeight = props.lineHeight,
          lineHeight = _props$lineHeight === void 0 ? 20 : _props$lineHeight,
          _props$gap = props.gap,
          gap = _props$gap === void 0 ? 50 : _props$gap;
      var _window = window,
          devicePixelRatio = _window.devicePixelRatio;

      if (devicePixelRatio < 1) {
        devicePixelRatio = 1;
      }

      fontSize *= devicePixelRatio;
      lineHeight *= devicePixelRatio;
      gap *= devicePixelRatio;

      if (rotation < -Math.PI / 2 || rotation > Math.PI / 2) {
        throw new Error('旋转角度应该介于-PI / 2 - PI / 2之间');
      }

      var canvas = this.canvas,
          ctx = this.ctx; // ////
      // document.body.appendChild(canvas);
      // canvas.style.position = 'fixed';
      // canvas.style.top = 0;
      // canvas.style.left = 0;
      // canvas.style.backgroundColor = 'white';
      // ////

      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.fillStyle = fontColor;
      ctx.font = "".concat(fontSize, "px ").concat(fontFamily);
      var contentArray = content.split('\n'); // @ts-ignore

      var wMatrix = [];
      var maxWidth = Math.ceil(contentArray.reduce(function (accumulator, currentValue) {
        var _ctx$measureText = ctx.measureText(currentValue),
            width = _ctx$measureText.width;

        wMatrix.push(width);

        if (width > accumulator) {
          return width;
        }

        return accumulator;
      }, 0));
      var maxHeight = lineHeight * contentArray.length;
      var w = maxWidth + gap * 2;
      var h = maxHeight + gap * 2;
      var r = rotation;

      if (r < 0) {
        r = -rotation;
      }

      canvas.height = Math.cos(r) * h + Math.sin(r) * w;
      canvas.width = Math.cos(r) * w + Math.sin(r) * h;
      ctx.translate(canvas.width / 2, canvas.height / 2);
      ctx.rotate(-rotation);
      var startY = (canvas.height - maxHeight) / 2;
      contentArray.forEach(function (line, index) {
        // @ts-ignore
        var startX = Math.round((canvas.width - wMatrix[index]) / 2);
        ctx.fillStyle = fontColor;
        ctx.font = "".concat(fontSize, "px ").concat(fontFamily);
        ctx.fillText(line, startX - canvas.width / 2, startY - canvas.height / 2 + (lineHeight - fontSize) / 2);
        startY += lineHeight;
      });
      var backgroundBase64 = canvas.toDataURL('image/png');
      var properties = {};
      properties.className = props.className;
      properties.style = _objectSpread(_objectSpread({}, props.style), {}, {
        position: 'relative'
      });
      return /*#__PURE__*/React.createElement("div", properties, children, /*#__PURE__*/React.createElement("div", {
        style: {
          background: "url('".concat(backgroundBase64, "') 0 0 repeat"),
          backgroundSize: "".concat(canvas.width / devicePixelRatio, "px ").concat(canvas.height / devicePixelRatio, "px"),
          position: 'absolute',
          left: 0,
          right: 0,
          top: 0,
          bottom: 0,
          zIndex: 99,
          pointerEvents: 'none',
          opacity: 0.4
        }
      }));
    }
  }]);

  return WaterMark;
}(Component);

export default WaterMark;