"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _instance = _interopRequireDefault(require("./instance.js"));

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

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

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["default"](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["default"].createElement("div", _extends({
      ref: function ref(el) {
        _this3.$el = el;
      }
    }, others, {
      className: "timepart" + (others.className ? ' ' + others.className : '')
    }));
  };

  return Timepart;
}(_react.Component);

exports["default"] = Timepart;

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

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