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;

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

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Instance from './instance.js';
import Bridge from './../Bridge';
import BridgeBrowser from './../Bridge/browser';
import Context from '../Context/instance.js';

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) {
          Bridge.showToast(locale('图片加载失败', 'hint_image_failed_to_load') + ', ' + locale('无法预览', 'cannot_preview'), { mask: false });
          return;
        }
        if (_this.props.isDrawSrc) {
          // 绘制背景
          layer = _this.instance.save();
          if (layer) {
            Bridge.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>';
          }
          BridgeBrowser.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 Instance(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 React.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 }),
      React.createElement(
        'div',
        { className: 'imgmark-loading active' },
        React.createElement('div', { className: 'imgmark-loading-icon' })
      ),
      React.createElement(
        'canvas',
        { ref: function ref(el) {
            _this2.$canvas = el;
          }, className: 'imgmark-wrapper', style: isDrawSrcStyle },
        'Canvas\u753B\u677F'
      ),
      React.createElement(
        'div',
        { className: 'imgmark-error' },
        React.createElement('div', { className: 'imgmark-error-icon' }),
        React.createElement(
          'div',
          { className: 'imgmark-error-caption' },
          locale('图片加载失败', 'hint_image_failed_to_load')
        )
      ),
      children
    );
  };

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

  onClick: PropTypes.func,

  preview: PropTypes.bool, // 是否预览

  children: PropTypes.node
} : {};