var _excluded = ["data", "src", "isDrawSrc", "watermark", "strokeStyle", "lineWidth", "quality", "style", "width", "height", "onClick", "preview", "children"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 = /*#__PURE__*/function (_Component) {
  _inheritsLoose(ImgMark, _Component);

  function ImgMark(props, context) {
    var _this;

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

    _defineProperty(_assertThisInitialized(_this), "validSrc", false);

    _defineProperty(_assertThisInitialized(_this), "fail", function () {
      _this.validSrc = false;
    });

    _defineProperty(_assertThisInitialized(_this), "success", function () {
      _this.validSrc = true;
    });

    _defineProperty(_assertThisInitialized(_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;
  }

  var _proto = ImgMark.prototype;

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

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

  _proto.render = function render() {
    var _this2 = this;

    // 全局配置
    var locale = this.context.locale;
    if (!locale) locale = function locale(remark) {
      return remark || '';
    };

    var _this$props = this.props,
        data = _this$props.data,
        src = _this$props.src,
        isDrawSrc = _this$props.isDrawSrc,
        watermark = _this$props.watermark,
        strokeStyle = _this$props.strokeStyle,
        lineWidth = _this$props.lineWidth,
        quality = _this$props.quality,
        style = _this$props.style,
        width = _this$props.width,
        height = _this$props.height,
        onClick = _this$props.onClick,
        preview = _this$props.preview,
        children = _this$props.children,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded);

    var isDrawSrcStyle = {};

    if (!isDrawSrc) {
      isDrawSrcStyle = {
        backgroundImage: "url(" + src + ")"
      };
    }

    return /*#__PURE__*/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
    }), /*#__PURE__*/React.createElement("div", {
      className: "imgmark-loading active"
    }, /*#__PURE__*/React.createElement("div", {
      className: "imgmark-loading-icon"
    })), /*#__PURE__*/React.createElement("canvas", {
      ref: function ref(el) {
        _this2.$canvas = el;
      },
      className: "imgmark-wrapper",
      style: isDrawSrcStyle
    }, "Canvas\u753B\u677F"), /*#__PURE__*/React.createElement("div", {
      className: "imgmark-error"
    }, /*#__PURE__*/React.createElement("div", {
      className: "imgmark-error-icon"
    }), /*#__PURE__*/React.createElement("div", {
      className: "imgmark-error-caption"
    }, locale('图片加载失败', 'hint_image_failed_to_load'))), children);
  };

  return ImgMark;
}(Component);

_defineProperty(ImgMark, "contextType", Context);

_defineProperty(ImgMark, "defaultProps", {
  isDrawSrc: false,
  strokeStyle: '#00ff00',
  lineWidth: 3,
  quality: 0.92,
  height: 300,
  preview: true
});

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