'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

var _Bridge = require('./../Bridge');

var _Bridge2 = _interopRequireDefault(_Bridge);

var _browser = require('./../Bridge/browser');

var _browser2 = _interopRequireDefault(_browser);

var _instance3 = require('../Context/instance.js');

var _instance4 = _interopRequireDefault(_instance3);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ImgMark = (_temp = _class = function (_Component) {
  _inherits(ImgMark, _Component);

  function ImgMark(props, context) {
    _classCallCheck(this, ImgMark);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));

    _this.validSrc = false;

    _this.fail = function () {
      _this.validSrc = false;
    };

    _this.success = function () {
      _this.validSrc = true;
    };

    _this.onClick = function () {
      var layer = ''; // 绘制的base64编码
      if (_this.props.preview) {
        if (!_this.validSrc) {
          _Bridge2.default.showToast(locale('图片加载失败', 'hint_image_failed_to_load') + ', ' + locale('无法预览', 'cannot_preview'), { mask: false });
          return;
        }
        if (_this.props.isDrawSrc) {
          // 绘制背景
          layer = _this.instance.save();
          if (layer) {
            _Bridge2.default.previewImage({ urls: [layer], index: 0 });
          }
        } else {
          // 不绘制背景
          layer = _this.instance.save();
          var previewHTML = '<div class="preview-layer" style="background-image:url(' + layer + ')"></div>';
          if (_this.props.watermark) {
            // 水印
            previewHTML += '<div class="preview-layer" style="background-image:url(' + _this.props.watermark + ');background-repeat: repeat; background-size:auto;"></div>';
          }
          _browser2.default.previewImage({ urls: [_this.props.src], layerHTML: previewHTML });
        }
      }
      if (_this.props.onClick) _this.props.onClick(layer);
    };

    return _this;
  }

  ImgMark.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.instance) {
      var params = {};
      if (prevProps.isDrawSrc !== this.props.isDrawSrc) {
        params.isDrawSrc = this.props.isDrawSrc;
      }
      if (prevProps.strokeStyle !== this.props.strokeStyle) {
        params.strokeStyle = this.props.strokeStyle;
      }
      if (prevProps.lineWidth !== this.props.lineWidth) {
        params.lineWidth = this.props.lineWidth;
      }
      if (prevProps.quality !== this.props.quality) {
        params.quality = this.props.quality;
      }
      if (prevProps.data !== this.props.data) {
        params.data = this.props.data;
      }
      if (Object.getOwnPropertyNames(params) && Object.getOwnPropertyNames(params).length) {
        this.instance.updateParams(params);
      }
    }
  };

  ImgMark.prototype.componentDidMount = function componentDidMount() {
    if (this.instance) return;
    this.instance = new _instance2.default(this.$canvas, {
      src: this.props.src,
      data: this.props.data,
      isDrawSrc: this.props.isDrawSrc,
      height: this.props.height,
      strokeStyle: this.props.strokeStyle,
      lineWidth: this.props.lineWidth,
      quality: this.props.quality,
      fail: this.fail,
      success: this.success
    });
  };

  ImgMark.prototype.render = function render() {
    var _this2 = this;

    // 全局配置
    var locale = this.context.locale;

    if (!locale) locale = function locale(remark) {
      return remark || '';
    };

    var _props = this.props,
        data = _props.data,
        src = _props.src,
        isDrawSrc = _props.isDrawSrc,
        watermark = _props.watermark,
        strokeStyle = _props.strokeStyle,
        lineWidth = _props.lineWidth,
        quality = _props.quality,
        style = _props.style,
        width = _props.width,
        height = _props.height,
        onClick = _props.onClick,
        preview = _props.preview,
        children = _props.children,
        others = _objectWithoutProperties(_props, ['data', 'src', 'isDrawSrc', 'watermark', 'strokeStyle', 'lineWidth', 'quality', 'style', 'width', 'height', 'onClick', 'preview', 'children']);

    var isDrawSrcStyle = {};
    if (!isDrawSrc) {
      isDrawSrcStyle = { backgroundImage: 'url(' + src + ')' };
    }
    return _react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, others, { className: 'imgmark' + (others.className ? ' ' + others.className : ''), style: Object({ width: width, height: height }, style || {}), onClick: this.onClick }),
      _react2.default.createElement(
        'div',
        { className: 'imgmark-loading active' },
        _react2.default.createElement('div', { className: 'imgmark-loading-icon' })
      ),
      _react2.default.createElement(
        'canvas',
        { ref: function ref(el) {
            _this2.$canvas = el;
          }, className: 'imgmark-wrapper', style: isDrawSrcStyle },
        'Canvas\u753B\u677F'
      ),
      _react2.default.createElement(
        'div',
        { className: 'imgmark-error' },
        _react2.default.createElement('div', { className: 'imgmark-error-icon' }),
        _react2.default.createElement(
          'div',
          { className: 'imgmark-error-caption' },
          locale('图片加载失败', 'hint_image_failed_to_load')
        )
      ),
      children
    );
  };

  return ImgMark;
}(_react.Component), _class.contextType = _instance4.default, _class.defaultProps = {
  isDrawSrc: false,
  strokeStyle: '#00ff00',
  lineWidth: 3,
  quality: 0.92,
  height: 300,
  preview: true
}, _temp);
exports.default = ImgMark;
ImgMark.propTypes = process.env.NODE_ENV !== "production" ? {
  // 数据源
  data: _propTypes2.default.array, // [{strokeStyle: '', lineWidth: '', setLineDash: [], x1: '', y1: '', x2: '', y2: ''}]
  src: _propTypes2.default.string,
  // canvas样式
  isDrawSrc: _propTypes2.default.bool, // 是否绘制背景
  watermark: _propTypes2.default.string,
  strokeStyle: _propTypes2.default.string,
  lineWidth: _propTypes2.default.number,
  quality: _propTypes2.default.number,
  style: _propTypes2.default.object,
  // 不能使用style制定宽高,canvas用style的width|height会导致绘图位置不正确
  width: _propTypes2.default.number, // 宽度
  height: _propTypes2.default.number, // 高度

  onClick: _propTypes2.default.func,

  preview: _propTypes2.default.bool, // 是否预览

  children: _propTypes2.default.node
} : {};
module.exports = exports['default'];