var _excluded = ["multiple", "startTime", "endTime", "times", "onChange", "onError"];

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

var Timepart = /*#__PURE__*/function (_Component) {
  _inheritsLoose(Timepart, _Component);

  function Timepart(props) {
    var _this;

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

    _defineProperty(_assertThisInitialized(_this), "update", function () {
      if (!_this.props.times) return;

      for (var i = 0, time; time = _this.props.times[i++];) {
        // eslint-disable-line
        if (time.className && time.startTime && time.endTime) {
          _this.instance.addProgress(time.startTime, time.endTime, time.className, time.data || null, time.cover || false);
        }
      } // onChange


      _this.onChange();
    });

    _defineProperty(_assertThisInitialized(_this), "onChange", function () {
      var times = _this.instance.getTimes();

      var part = _this.instance.container.querySelector('.timepart-part.active');

      if (part) {
        times.push({
          className: part.className.replace('timepart-part ', ''),
          startTime: part.startTime,
          endTime: part.endTime,
          data: part.getAttribute('data') || ''
        });
      }

      if (_this.props.onChange) _this.props.onChange(times);
    });

    return _this;
  }

  var _proto = Timepart.prototype;

  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.props.times && prevProps.times.length !== this.props.times.length) {
      this.update();
    }
  };

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

    var clickCount = 0;
    var instance = new Instance(this.$el, {
      startTime: this.props.startTime,
      endTime: this.props.endTime,
      onClickPart: function onClickPart(s) {
        if (s.target.classList.contains('active')) {
          s.target.classList.remove('active');
          clickCount = 0; // onChange

          _this2.onChange();

          return;
        } // 如果不允许多选,发现有已选中的先清除


        if (!_this2.props.multiple) {
          if (s.container.querySelectorAll('.progress-legend.active').length) {
            s.removeProgress('active');
            clickCount = 0; // onChange

            _this2.onChange();

            return;
          }
        } // 选中


        clickCount++;

        if (clickCount === 1) {
          // 如果点击了一次
          _this2.part1 = s.target;

          _this2.part1.classList.add('active'); // onChange


          _this2.onChange();
        } else if (clickCount === 2) {
          // 如果点击了两次
          _this2.part1.classList.remove('active');

          _this2.part2 = s.target;
          var times = s.getTimesByParts(_this2.part1, _this2.part2);
          s.addProgress(times.startTime, times.endTime, 'active');
          clickCount = 0; // onChange

          _this2.onChange();
        }
      },
      onContain: function onContain(e) {
        clickCount = 0;
        if (_this2.props.onError) _this2.props.onError(e, {
          errMsg: '已包含其它时间段'
        });
      },
      onCross: function onCross(e) {
        if (_this2.props.onError) _this2.props.onError(e, {
          errMsg: '与其它时间段相交'
        });
      },
      onClickProgress: function onClickProgress(s) {
        if (s.target.classList.contains('active')) {
          // 根据data-id删除
          var id = s.target.getAttribute('data-id');
          [].slice.call(s.container.querySelectorAll(".timepart-progress[data-id=\"" + id + "\"]")).forEach(function (el) {
            el.parentNode.removeChild(el);
          }); // onChange

          _this2.onChange();
        }
      },
      onClickWhite: function onClickWhite(s) {}
    });
    this.instance = instance;
    this.update();
  };

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

    var _this$props = this.props,
        multiple = _this$props.multiple,
        startTime = _this$props.startTime,
        endTime = _this$props.endTime,
        times = _this$props.times,
        onChange = _this$props.onChange,
        onError = _this$props.onError,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded);

    return /*#__PURE__*/React.createElement("div", _extends({
      ref: function ref(el) {
        _this3.$el = el;
      }
    }, others, {
      className: "timepart" + (others.className ? ' ' + others.className : '')
    }));
  };

  return Timepart;
}(Component);

_defineProperty(Timepart, "defaultProps", {
  startTime: '07:00',
  endTime: '22:00'
});

export { Timepart as default };
Timepart.propTypes = process.env.NODE_ENV !== "production" ? {
  multiple: PropTypes.bool,
  // 是否支持多选
  startTime: PropTypes.string,
  endTime: PropTypes.string,
  times: PropTypes.array,
  // [{className: string, startTime: 'hh:ss', endTime: 'hh:ss', data: string, cover: bool}]
  onChange: PropTypes.func,
  // onChange(times)
  onError: PropTypes.func
} : {};