"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _tslib = require("tslib");

var _consoleComponents = require("@alicloud/console-components");

var _moment = _interopRequireDefault(require("moment"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var Option = _consoleComponents.Select.Option;
var RangePicker = _consoleComponents.DatePicker.RangePicker;

var getSecondTime = function getSecondTime(count) {
  return parseInt("" + count / 60, 0) * 60;
};

var LPRangePicker =
/** @class */
function (_super) {
  (0, _tslib.__extends)(LPRangePicker, _super);

  function LPRangePicker(props) {
    var _this = _super.call(this, props) || this;

    _this.setTime = function (timeStart, timeEnd, v) {
      _this.setState({
        StartTime: timeStart,
        EndTime: timeEnd,
        showRangePick: false
      }, function () {
        _this.props.onChange(timeStart, timeEnd, v);

        _this.props.getTimeRange(timeStart, timeEnd, v);
      });
    };

    _this.timeSelect = function (v) {
      var initTime = parseInt((0, _moment["default"])().format('X'), 0);
      var timeEnd = _this.props.disabledSecond ? getSecondTime(initTime) * 1000 : initTime * 1000;
      var timeStart;

      if (v !== "custom_rangPaick") {
        timeStart = timeEnd - (v ? v : 0);

        _this.setTime(timeStart, timeEnd, v);
      } else {
        _this.setState({
          showRangePick: true,
          defaultValue: undefined
        });

        _this.props.onChange(undefined, undefined, []);
      }
    };

    _this.rangeChange = function (v) {
      var time = v.map(function (val) {
        return val ? (0, _moment["default"])(val).format('x') : 0;
      });

      _this.setState({
        StartTime: time[0],
        EndTime: time[1]
      });

      _this.props.pickChange && _this.props.pickChange(v);
    };

    _this.transLateSelectValue = function (item) {
      var contents = '';

      if (typeof item === 'string') {
        contents = item.split('-');
      } else if (_typeof(item) === 'object') {
        contents = item.label.split('-');
      }

      var name = '',
          value;

      switch (contents[1]) {
        case 'min':
          name = contents[0] + '分钟';
          value = contents[0] * 60 * 1000;
          break;

        case 'hour':
          name = contents[0] + '小时';
          value = contents[0] * 60 * 60 * 1000;
          break;

        case 'day':
          name = contents[0] + '天';
          value = contents[0] * 24 * 60 * 60 * 1000;
          break;

        default:
          name = item.label ? item.label : item;
          value = item.value ? item.value : 0;
      }

      if (item === 'custom' || item.label === 'custom') {
        name = '自定义';
        value = "custom_rangPaick";
      }

      return {
        name: name,
        value: value
      };
    };

    _this.RenderOption = function () {
      _this.selectOption = [];
      return _this.props.option.map(function (item) {
        var optionObj = _this.transLateSelectValue(item);

        return /*#__PURE__*/_react["default"].createElement(Option, {
          key: 'timeRange' + optionObj.value,
          value: optionObj.value,
          disabled: item.disable
        }, optionObj.name);
      });
    };

    var initTime = parseInt((0, _moment["default"])().format('X'), 0);
    var initEnd = props.disabledSecond ? getSecondTime(initTime) * 1000 : initTime * 1000;
    var initStart = initEnd - 60 * 60 * 1000;
    _this.state = {
      EndTime: initEnd,
      StartTime: initStart,
      showRangePick: false,
      defaultValue: undefined
    };
    _this.selectOption = [];
    _this.randomId = Math.random();
    return _this;
  }

  LPRangePicker.prototype.componentDidMount = function () {
    if (Array.isArray(this.props.defaultValue)) {
      this.setState({
        showRangePick: true,
        defaultValue: this.props.defaultValue
      });
    }
  };

  LPRangePicker.prototype.render = function () {
    var _this = this;

    var showRange = null;

    if (Object.keys(this.props).indexOf("value") !== -1) {
      //受控
      Array.isArray(this.props.value) ? // 当选择自定义的时候
      showRange = /*#__PURE__*/_react["default"].createElement(RangePicker, {
        key: "rangePicker-2-" + this.randomId,
        format: this.props.Dateformat,
        style: {
          verticalAlign: 'top'
        },
        showTime: true,
        defaultValue: this.props.value,
        onChange: this.rangeChange,
        state: this.props.state,
        onOk: function onOk() {
          _this.props.onChange(_this.state.StartTime, _this.state.EndTime, [_this.state.StartTime, _this.state.EndTime]);

          _this.props.getTimeRange(_this.state.StartTime, _this.state.EndTime, _this.state.EndTime - _this.state.StartTime);
        },
        onVisibleChange: function onVisibleChange(v, t) {
          _this.props.onVisibleChange && _this.props.onVisibleChange(v, t);
        },
        disabledDate: this.props.disabledDate
      }) : null;
      return [/*#__PURE__*/_react["default"].createElement(_consoleComponents.Select, {
        key: "rangePicker-1-" + this.randomId,
        hasClear: true,
        style: {
          marginRight: 8
        },
        value: Array.isArray(this.props.value) ? 'custom_rangPaick' : this.props.value,
        onChange: this.timeSelect
      }, this.RenderOption()), showRange];
    } else {
      //非受控
      this.state.showRangePick ? // 当选择自定义的时候
      showRange = /*#__PURE__*/_react["default"].createElement(RangePicker, {
        key: "rangePicker-2-" + this.randomId,
        format: this.props.Dateformat,
        style: {
          verticalAlign: 'top'
        },
        defaultValue: this.state.defaultValue,
        showTime: true,
        onChange: this.rangeChange,
        state: this.props.state,
        onOk: function onOk() {
          _this.props.onChange(_this.state.StartTime, _this.state.EndTime, 'custiom');
        },
        onVisibleChange: function onVisibleChange(v, t) {
          _this.props.onVisibleChange && _this.props.onVisibleChange(v, t);
        },
        disabledDate: this.props.disabledDate
      }) : null;
      return [/*#__PURE__*/_react["default"].createElement(_consoleComponents.Select, {
        key: "rangePicker-1-" + this.randomId,
        hasClear: true,
        style: {
          marginRight: 8
        },
        defaultValue: Array.isArray(this.props.defaultValue) ? 'custom_rangPaick' : this.props.defaultValue,
        onChange: this.timeSelect
      }, this.RenderOption()), showRange];
    }
  };

  return LPRangePicker;
}(_react.Component);

var _default = LPRangePicker;
exports["default"] = _default;
LPRangePicker.defaultProps = {
  onChange: function onChange() {},
  getTimeRange: function getTimeRange() {},
  disabledSecond: true,
  Dateformat: "YYYY-MM-DD",
  option: [{
    label: '15-min',
    disable: false
  }, {
    label: '1-hour',
    disable: false
  }, {
    label: '4-hour',
    disable: false
  }, {
    label: '24-hour',
    disable: false
  }, {
    label: '7-day',
    disable: false
  }, {
    label: 'custom',
    disable: false
  }]
};
;