var Element = require("./Element");

var dataUtil = require("../core/utils/data_structure_util");

var textContain = require("../core/contain/text");

var textUtil = require("./utils/text_util");

var _constants = require("./constants");

var ContextCachedBy = _constants.ContextCachedBy;

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

/**
 * @class qrenderer.graphic.Text
 * 
 * @docauthor 大漠穷秋 <damoqiongqiu@126.com>
 */
var Text =
/*#__PURE__*/
function (_Element) {
  _inherits(Text, _Element);

  /**
   * @method constructor Text
   * @param {Object} opts 
   */
  function Text(opts) {
    var _this;

    _classCallCheck(this, Text);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Text).call(this, opts));
    /**
     * @property {String} type
     */

    _this.type = 'text';
    return _this;
  }

  _createClass(Text, [{
    key: "brush",
    value: function brush(ctx, prevEl) {
      var style = this.style; // Optimize, avoid normalize every time.

      this.__dirty && textUtil.normalizeTextStyle(style, true); // Use props with prefix 'text'.

      style.fill = style.stroke = style.shadowBlur = style.shadowColor = style.shadowOffsetX = style.shadowOffsetY = null;
      var text = style.text; // Convert to string

      text != null && (text += ''); // Do not apply style.bind in Text node. Because the real bind job
      // is in textUtil.renderText, and performance of text render should
      // be considered.
      // style.bind(ctx, this, prevEl);

      if (!textUtil.needDrawText(text, style)) {
        // The current el.style is not applied
        // and should not be used as cache.
        ctx.__attrCachedBy = ContextCachedBy.NONE;
        return;
      }

      this.setTransform(ctx);
      textUtil.renderText(this, ctx, text, style, null, prevEl);
      this.restoreTransform(ctx);
    }
  }, {
    key: "getBoundingRect",
    value: function getBoundingRect() {
      var style = this.style; // Optimize, avoid normalize every time.

      this.__dirty && textUtil.normalizeTextStyle(style, true);

      if (!this._rect) {
        var text = style.text;
        text != null ? text += '' : text = '';
        var rect = textContain.getBoundingRect(style.text + '', style.font, style.textAlign, style.textVerticalAlign, style.textPadding, style.textLineHeight, style.rich);
        rect.x += style.x || 0;
        rect.y += style.y || 0;

        if (textUtil.getStroke(style.textStroke, style.textStrokeWidth)) {
          var w = style.textStrokeWidth;
          rect.x -= w / 2;
          rect.y -= w / 2;
          rect.width += w;
          rect.height += w;
        }

        this._rect = rect;
      }

      return this._rect;
    }
  }]);

  return Text;
}(Element);

module.exports = Text;