"use strict";

exports.__esModule = true;
exports.default = exports.customWatermark = void 0;

// 添加自定义水印
var customWatermark = function customWatermark(text, el, color, rotate) {
  if (text === void 0) {
    text = 'KYE';
  }

  if (el === void 0) {
    el = '.ksui-container, .van-dialog, .ksui-card, .ks-table';
  }

  if (color === void 0) {
    color = 'rgba(0,0,0,0.1)';
  }

  if (rotate === void 0) {
    rotate = 30;
  }

  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  ctx.font = '100 12px PingFangSC'; // 角度转弧度

  rotate = rotate * Math.PI / 180;
  var text1 = '';
  var text2 = ''; // text参数是数组则文本分两行

  if (Array.isArray(text)) {
    text1 = text[0];
    text2 = text[1];
  } else {
    text1 = text;
  }

  var text1Width = ctx.measureText(text1).width;
  var text2Width = ctx.measureText(text2).width;
  var l = text1Width > text2Width ? text1Width : text2Width; // 将文本长度作为斜边求两个直角边

  var top = Math.floor(l * Math.sin(rotate));
  var left = Math.floor(l * Math.cos(rotate));
  var dpr = window.devicePixelRatio;
  var cssWidth = left + 50;
  var cssHeight = top + 80; // canvas.style.width = cssWidth + 'px'
  // canvas.style.height = cssHeight + 'px'

  canvas.width = cssWidth * dpr;
  canvas.height = cssHeight * dpr;
  ctx.scale(dpr, dpr);
  ctx.fillStyle = color;
  ctx.textBaseline = 'middle';
  ctx.rotate(-rotate);
  ctx.fillText(text1, 0, top);
  ctx.fillText(text2, 0, top + 10);
  var encode = canvas.toDataURL('image/png');
  var style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = el + "{background-image:url(" + encode + ");background-size: " + cssWidth + "px " + cssHeight + "px;}";
  document.querySelector('head').appendChild(style);
};
/**
 * 添加水印方法，调用ksui.watermark
 * @param {string} employeeNumber 工号
 * @param {sting} el css选择器
 * @param {string} color 水印文字颜色（格式rgba）
 */


exports.customWatermark = customWatermark;

var watermark = function watermark(employeeNumber, el, color) {
  if (el === void 0) {
    el = '.ksui-container, .van-dialog, .ksui-card, .ks-table';
  }

  if (color === void 0) {
    color = '#F0F3F9';
  }

  var date = new Date();

  var f = function f(num) {
    var str = String(num);
    return str.length === 1 ? '0' + str : str;
  };

  var text = ["KYE" + employeeNumber, date.getFullYear() + "/" + f(date.getMonth() + 1) + "/" + f(date.getDate()) + " " + f(date.getHours()) + ":" + f(date.getMinutes()) + ":" + f(date.getSeconds())];
  customWatermark(text, el, color);
};

var _default = watermark;
exports.default = _default;