"use strict";

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

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

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

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

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

var _datetimerangepickercomponent = _interopRequireDefault(require("../../helpers/datetimerangepickercomponent"));

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function transformAttrs(props) {
  var _props$attributes = props.attributes,
      startdatefieldname = _props$attributes.startdatefieldname,
      enddatefieldname = _props$attributes.enddatefieldname,
      enableranges = _props$attributes.enableranges;
  var startDate = props.attributes[startdatefieldname];
  var endDate = props.attributes[enddatefieldname];
  var modifiedAttrs = {
    startDate: startDate ? (0, _moment["default"])(startDate) : (0, _moment["default"])().startOf('day').add(7, 'hours'),
    endDate: endDate ? (0, _moment["default"])(endDate) : (0, _moment["default"])().endOf('day')
  };

  if (enableranges) {
    modifiedAttrs.ranges = {
      Today: [(0, _moment["default"])().startOf('day'), (0, _moment["default"])()],
      Yesterday: [(0, _moment["default"])().subtract(1, 'days').startOf('day'), (0, _moment["default"])().subtract(1, 'days').endOf('day')],
      'Last 7 Days': [(0, _moment["default"])().subtract(6, 'days').startOf('day'), (0, _moment["default"])().endOf('day')],
      'Last 30 Days': [(0, _moment["default"])().subtract(29, 'days').startOf('day'), (0, _moment["default"])().endOf('day')],
      'This Month': [(0, _moment["default"])().startOf('month'), (0, _moment["default"])().endOf('month')],
      'Last Month': [(0, _moment["default"])().subtract(1, 'month').startOf('month'), (0, _moment["default"])().subtract(1, 'month').endOf('month')]
    };
  }

  return modifiedAttrs;
}

var DatetimeRangePicker = /*#__PURE__*/function (_React$Component) {
  _inherits(DatetimeRangePicker, _React$Component);

  var _super = _createSuper(DatetimeRangePicker);

  function DatetimeRangePicker(props) {
    var _this;

    _classCallCheck(this, DatetimeRangePicker);

    _this = _super.call(this, props);
    _this.handleEvent = _this.handleEvent.bind(_assertThisInitialized(_this));
    _this.state = props ? transformAttrs(props) : {};
    return _this;
  }

  _createClass(DatetimeRangePicker, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(props) {
      this.setState(transformAttrs(props));
    }
  }, {
    key: "handleEvent",
    value: function handleEvent(event, picker) {
      var state = this.state;
      var props = this.props;
      var startDate = state.startDate,
          endDate = state.endDate;

      if ((startDate !== picker.startDate || endDate !== picker.endDate) && event.type !== 'show') {
        var dates = {
          startDate: picker.startDate,
          endDate: picker.endDate
        };
        this.setState(dates);
        props.onChange(props.control, picker);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var state = this.state;
      var start = state.startDate.format('MMMM D, YYYY');
      var end = state.endDate.format('MMMM D, YYYY');
      var label = "".concat(start, " - ").concat(end);
      if (start === end) label = start;
      var attributes = props.attributes;
      var containerStyle = attributes.containerStyle,
          buttonStyle = attributes.buttonStyle;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "bootstrap_style form-group",
        style: containerStyle || {
          padding: '10px'
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "date-range-picker"
      }, /*#__PURE__*/_react["default"].createElement(_datetimerangepickercomponent["default"], _extends({
        startDate: state.startDate,
        endDate: state.endDate,
        onApply: this.handleEvent
      }, attributes, {
        ranges: attributes.enableranges && state.ranges
      }), /*#__PURE__*/_react["default"].createElement("button", {
        type: "button",
        className: "selected-date-range-btn btn btn-primary font-awesome",
        style: buttonStyle || {
          width: '100%',
          fontSize: '18px',
          fontWeight: 500,
          backgroundColor: '#1681FF'
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "pull-left"
      }, /*#__PURE__*/_react["default"].createElement("i", {
        className: "fa fa-calendar"
      }), "\xA0", /*#__PURE__*/_react["default"].createElement("span", null, label)), /*#__PURE__*/_react["default"].createElement("div", {
        className: "pull-right"
      }, /*#__PURE__*/_react["default"].createElement("i", {
        className: "fa fa-angle-down"
      }))))));
    }
  }]);

  return DatetimeRangePicker;
}(_react["default"].Component);

DatetimeRangePicker.propTypes = process.env.NODE_ENV !== "production" ? {
  library: _propTypes["default"].object,
  component: _propTypes["default"].string.isRequired,
  attributes: _propTypes["default"].object,
  control: _propTypes["default"].object,
  option: _propTypes["default"].string.isRequired,
  rules: _propTypes["default"].object,
  onChange: _propTypes["default"].func
} : {};
DatetimeRangePicker.defaultProps = {
  library: null,
  attributes: null,
  control: null,
  rules: null,
  onChange: null
};
var _default = DatetimeRangePicker;
exports["default"] = _default;