import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { getImageSize } from '../third-party/image-size.mjs';
import { getCanvas } from './get-canvas.mjs';
import '../third-party/canvas.mjs';

/**
 * 为图片增加文字
 *
 * @param {Object} config 配置
 * @param {number} config.width 宽度
 * @param {number} config.height 高度
 * @param {Array<string>} config.textList 文字列表，支持多行
 * @param {string} config.imgPath 图片路径
 * @returns {string} canvas.toDataURL生成的base64图片
 *
 * @example
 *
 * ```ts
 * const imgUrl = addTextForImg({
 *   width: 300,
 *   height: 300,
 *   textList: ['第一行', '第二行'],
 *   imgPath: './test.png',
 * })
 * ```
 */
function addTextForImg(_ref) {
  var width = _ref.width,
    height = _ref.height,
    textList = _ref.textList,
    imgPath = _ref.imgPath;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var canvasLibrary, sizeOf, createCanvas, loadImage, image, lineHeight, dpr, topHeight, dimensions, oHeight, oWidth, canvas, ctx, drawBackground, fillTitle, imgUrl;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          fillTitle = function _fillTitle(textList) {
            ctx.lineWidth = 1;
            ctx.strokeStyle = '#ccc';
            ctx.textAlign = 'start';
            ctx.font = '10px Arial';
            ctx.fillStyle = '#000';
            for (var j = 0; j < textList.length; j++) {
              ctx.fillText(textList[j], 15, 20 + j * lineHeight);
            }
          };
          drawBackground = function _drawBackground() {
            ctx.fillStyle = '#fff';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
          };
          canvasLibrary = getCanvas();
          if (canvasLibrary) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return", '');
        case 1:
          sizeOf = getImageSize();
          createCanvas = canvasLibrary.createCanvas, loadImage = canvasLibrary.loadImage;
          _context.next = 2;
          return loadImage(imgPath);
        case 2:
          image = _context.sent;
          lineHeight = 15;
          dpr = 2;
          topHeight = textList.length * lineHeight + 15;
          dimensions = sizeOf(imgPath);
          oHeight = dimensions.height, oWidth = dimensions.width;
          if (!width) {
            width = oWidth;
          }
          if (!height) {
            height = oHeight;
          }
          canvas = createCanvas(width * dpr, (height + topHeight) * dpr);
          ctx = canvas.getContext('2d');
          ctx.scale(dpr, dpr);
          drawBackground();
          ctx.drawImage(image, 0, topHeight, width, width);
          fillTitle(textList);
          imgUrl = canvas.toDataURL();
          return _context.abrupt("return", imgUrl);
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

export { addTextForImg };
