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 BridgeBrowser from './../Bridge/browser';

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

  // 全局配置
  function Vott(props, context) {
    _classCallCheck(this, Vott);

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

    _this.instance = function () {
      var _this$props = _this.props,
          data = _this$props.data,
          readOnly = _this$props.readOnly,
          src = _this$props.src,
          _this$props$params = _this$props.params,
          params = _this$props$params === undefined ? {} : _this$props$params;

      _this.instance = new Instance(_this.$el, _extends({
        readOnly: readOnly,
        data: data,
        src: src,
        success: _this.onLoad
      }, params));
    };

    _this.onLoad = function () {
      _this.load = 1;
    };

    _this.onClick = function () {
      _this.preview();
    };

    _this.preview = function () {
      if (!_this.props.readOnly || !_this.props.preview || !_this.load) return;
      var svg = _this.instance.svg;
      var previewHTML = '<div class="preview-layer">' + svg.outerHTML + '</div>';
      BridgeBrowser.previewImage({ urls: [_this.props.src], layerHTML: previewHTML, success: function success(s) {
          var layer = s.container.querySelector('.preview-layer');
          svg = s.container.querySelector('.vott-svg');
          svg.style.backgroundImage = 'initial';
          var width = svg.style.width.replace('px', '');
          var height = svg.style.height.replace('px', '');
          // 计算宽高
          var scale = 1;
          if (width > height) {
            // 宽图计算
            scale = layer.clientWidth / width;
          } else {
            // 长图计算
            scale = layer.clientHeight / height;
          }
          svg.style.WebkitTransform = 'scale(' + scale + ') translate(-50%,-50%)';
          svg.style.WebkitTransformOrigin = '0 0';
        } });
    };

    return _this;
  }
  // data = {
  //   polygon: [ // 逆时针
  //     [x2, y1], // 右上
  //     [x1, y1], // 左上
  //     [x1, y2], // 左下
  //     [x2, y2], // 右下
  //   ],
  //   style: '',
  //   className: '',
  //   id: '',
  //   ...
  // }
  // params = {
  //   shapeAttributes: '形状属性'
  // }


  Vott.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.instance) {
      if (prevProps.params !== this.props.params) {
        this.instance.updateParams(this.props.params);
      }
      if (prevProps.readOnly !== this.props.readOnly) {
        this.instance.setReadOnly(this.props.readOnly);
      }
      if (prevProps.src !== this.props.src) {
        this.instance.updateParams({ src: this.props.src });
        this.load = 0;
        this.instance.update();
      }
      if (prevProps.data !== this.props.data) {
        this.instance.updateParams({ data: this.props.data });
        this.load = 0;
        this.instance.update();
      }
    }
  };

  Vott.prototype.componentDidMount = function componentDidMount() {
    this.instance();
  };

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

    // 全局配置
    var _context$locale = this.context.locale,
        locale = _context$locale === undefined ? {} : _context$locale;

    var _props = this.props,
        data = _props.data,
        readOnly = _props.readOnly,
        src = _props.src,
        _props$params = _props.params,
        params = _props$params === undefined ? {} : _props$params,
        preview = _props.preview,
        others = _objectWithoutProperties(_props, ['data', 'readOnly', 'src', 'params', 'preview']);

    return React.createElement(
      'div',
      _extends({ className: 'vott-container' }, others, { ref: function ref(el) {
          _this2.$el = el;
        } }),
      React.createElement('svg', { className: 'vott-svg', preserveAspectRatio: 'none', onClick: this.onClick }),
      React.createElement(
        'div',
        { className: 'vott-loading active' },
        React.createElement('div', { className: 'vott-loading-icon' })
      ),
      React.createElement(
        'div',
        { className: 'vott-error' },
        React.createElement('div', { className: 'vott-error-icon' }),
        React.createElement(
          'div',
          { className: 'vott-error-caption' },
          locale['hint_image_failed_to_load'] || '图片加载失败'
        )
      )
    );
  };

  return Vott;
}(Component), _class.contextTypes = {
  locale: PropTypes.object,
  portal: PropTypes.object
}, _class.defaultProps = {}, _temp);
export { Vott as default };
Vott.propTypes = process.env.NODE_ENV !== "production" ? {
  data: PropTypes.array, // 渲染形状: [{polygon: [[x,y]], css: '', class: ''}]
  readOnly: PropTypes.bool, // 是否只读
  src: PropTypes.string,
  params: PropTypes.object, // 设置实例化参数
  preview: PropTypes.bool // 是否支持单击预览, readOnly为true时才生效
} : {};