"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;
exports.setSize = exports.getOpenData = exports.fittingString = void 0;

var _openData = require("../../utils/open-data");

var _bsGlobal = require("../../utils/bs-global");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _default(_x, _x2, _x3) {
  return _ref.apply(this, arguments);
}

function _ref() {
  _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(ctx, openDataParams, lineHeight) {
    var maxWidth,
        name,
        _ctx$measureText,
        width,
        actualWidth,
        _args = arguments;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            maxWidth = _args.length > 3 && _args[3] !== undefined ? _args[3] : Infinity;
            name = openDataParams.name;

            if (!(0, _bsGlobal.isWx)()) {
              _context.next = 6;
              break;
            }

            _context.next = 5;
            return getOpenData(openDataParams);

          case 5:
            name = _context.sent;

          case 6:
            _ctx$measureText = ctx.measureText(name), width = _ctx$measureText.width;
            actualWidth = Math.min(width, maxWidth);
            return _context.abrupt("return", {
              width: actualWidth,
              height: lineHeight,
              draw: function draw(ctx, x, y) {
                ctx.save();
                ctx.textBaseline = 'middle';
                ctx.beginPath();
                ctx.rect(x, y, actualWidth, lineHeight);
                ctx.clip();
                ctx.fillText(name, x, y + lineHeight / 2);
                ctx.closePath();
                ctx.restore();
              }
            });

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _ref.apply(this, arguments);
}

var getOpenData = function getOpenData(_ref2) {
  var id = _ref2.id,
      name = _ref2.name,
      type = _ref2.type,
      openId = _ref2.openId;
  openId = openId || (0, _openData.getOpenId)(id, type);
  if (!window.WWOpenData || !openId) return Promise.resolve(name);
  return new Promise(function (resolve, reject) {
    window.WWOpenData.prefetch({
      items: [{
        id: openId,
        type: type
      }]
    }, function (err, data) {
      if (err) {
        return reject(err);
      }

      if (data && data.items[0]) {
        resolve(data.items[0].data);
      } else {
        resolve(name || '');
      }
    });
  });
};

exports.getOpenData = getOpenData;

var setSize = function setSize(canvas, ctx, width, height) {
  var devicePixelRatio = window.devicePixelRatio || 1;
  var data = ctx.getImageData(0, 0, width, height); // 要将 canvas 的宽高设置成容器宽高的 2 倍

  canvas.width = width * devicePixelRatio;
  canvas.height = height * devicePixelRatio;
  canvas.style.width = "".concat(width, "px");
  canvas.style.height = "".concat(height, "px"); // 然后将画布缩放，将图像放大两倍画到画布上

  ctx.scale(devicePixelRatio, devicePixelRatio);
  ctx.putImageData(data, 0, 0);
};

exports.setSize = setSize;

var fittingString = function fittingString(_ctx, str, maxWidth) {
  var strWidth = _ctx.measureText(str).width;

  var ellipsis = '…';

  var ellipsisWidth = _ctx.measureText(ellipsis).width;

  if (strWidth <= maxWidth || maxWidth <= ellipsisWidth) {
    return str;
  }

  var len = str.length;

  while (strWidth >= maxWidth - ellipsisWidth && len-- > 0) {
    str = str.slice(0, len);
    strWidth = _ctx.measureText(str).width;
  }

  return str + ellipsis;
};

exports.fittingString = fittingString;