"use strict";

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

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