'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MaterialIcon = exports.SlotitemIcon = undefined;

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

const getClassName = (props, isSVG) => {
  const type = isSVG ? 'svg' : 'png';
  return (0, _classnames2.default)(type, props);
};

const { ROOT, config } = window;

const svgAvailableList = {};
const pngAvailableList = {};

class iconConf {
  constructor() {
    this.setConf = val => {
      this.callbacks.forEach(f => f(val));
    };

    this.reg = (key, func) => {
      this.callbacks.set(key, func);
    };

    this.unreg = key => {
      this.callbacks.delete(key);
    };

    this.callbacks = new Map();
  }
}

const iconConfSetter = new iconConf();

const setIcon = (path, val) => {
  if (path === 'poi.useSVGIcon') {
    iconConfSetter.setConf(val);
  }
};

config.addListener('config.set', setIcon);

window.addEventListener('unload', e => {
  config.removeListener('config.set', setIcon);
});

class SlotitemIcon extends _react.PureComponent {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      useSVGIcon: config.get('poi.useSVGIcon', false)
    }, this.name = 'SlotitemIcon', this.svgPath = () => `${ROOT}/assets/svg/slotitem/${this.props.slotitemId}.svg`, this.pngPath = () => `${ROOT}/assets/img/slotitem/${this.props.slotitemId + 100}.png`, this.getAvailable = () => {
      try {
        _fsExtra2.default.statSync(this.state.useSVGIcon ? this.svgPath() : this.pngPath());
        return true;
      } catch (e) {
        return false;
      }
    }, this.setUseSvg = val => {
      this.setState({
        useSVGIcon: val
      });
    }, this.componentDidMount = () => {
      this.key = `${process.hrtime()[0]}${process.hrtime()[1]}`;
      iconConfSetter.reg(this.key, this.setUseSvg);
    }, this.componentWillUnmount = () => {
      iconConfSetter.unreg(this.key);
    }, _temp;
  }

  render() {
    const { alt, slotitemId, className } = this.props;
    if (this.state.useSVGIcon) {
      if (typeof svgAvailableList[slotitemId] === 'undefined') {
        svgAvailableList[slotitemId] = this.getAvailable();
      }
    } else {
      if (typeof pngAvailableList[slotitemId] === 'undefined') {
        pngAvailableList[slotitemId] = this.getAvailable();
      }
    }
    if (this.state.useSVGIcon) {
      return svgAvailableList[slotitemId] ? _react2.default.createElement('img', { alt: alt, src: `file://${this.svgPath()}`, className: getClassName(className, true) }) : _react2.default.createElement('img', { alt: alt, src: `file://${ROOT}/assets/svg/slotitem/-1.svg`, className: getClassName(className, true) });
    } else if (pngAvailableList[slotitemId]) {
      return _react2.default.createElement('img', { alt: alt, src: `file://${this.pngPath()}`, className: getClassName(className, false) });
    } else {
      return _react2.default.createElement('img', { alt: alt, src: `file://${ROOT}/assets/img/slotitem/-1.png`, className: getClassName(className, false) });
    }
  }
}

exports.SlotitemIcon = SlotitemIcon;
SlotitemIcon.propTypes = {
  slotitemId: _propTypes2.default.number,
  className: _propTypes2.default.string,
  alt: _propTypes2.default.string
};
class MaterialIcon extends _react.PureComponent {
  constructor(...args) {
    var _temp2;

    return _temp2 = super(...args), this.state = {
      useSVGIcon: config.get('poi.useSVGIcon', false)
    }, this.name = 'MaterialIcon', this.setUseSvg = val => {
      this.setState({
        useSVGIcon: val
      });
    }, this.componentDidMount = () => {
      this.key = `${process.hrtime()[0]}${process.hrtime()[1]}`;
      iconConfSetter.reg(this.key, this.setUseSvg);
    }, this.componentWillUnmount = () => {
      iconConfSetter.unreg(this.key);
    }, _temp2;
  }

  render() {
    let src = null;
    if (this.state.useSVGIcon) {
      src = `file://${ROOT}/assets/svg/material/${this.props.materialId}.svg`;
    } else {
      src = `file://${ROOT}/assets/img/material/0${this.props.materialId}.png`;
    }
    return _react2.default.createElement('img', { alt: this.props.alt, src: src, className: getClassName(this.props.className, this.state.useSVGIcon) });
  }
}
exports.MaterialIcon = MaterialIcon;
MaterialIcon.propTypes = {
  materialId: _propTypes2.default.number,
  className: _propTypes2.default.string,
  alt: _propTypes2.default.string
};