'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.DatePickerControl = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

var _FormControl2 = require("./FormControl");

var _lodash = _interopRequireDefault(require("lodash"));

var _BSDatePicker = require("./BSDatePicker");

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

var _utils = require("../../../utils");

var _classnames = _interopRequireDefault(require("classnames"));

var _Icon = require("../Icon");

var DatePickerControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(DatePickerControl, _FormControl);

  function DatePickerControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = DatePickerControl.prototype;

  _proto.renderDay = function renderDay(props, currentDate) {
    var day = currentDate.date();
    var selected = props.className.indexOf('rdtActive') !== -1;
    var active = !this.props.disabled && props.className.indexOf('rdtOld') === -1 && props.className.indexOf('rdtNew') === -1 && props.className.indexOf('rdtDisabled') === -1;
    return _react.default.createElement("td", (0, _extends2.default)({
      role: active ? 'button' : 'presentation',
      tabIndex: active ? 0 : undefined,
      "aria-label": active ? day : undefined,
      "aria-selected": selected ? 'true' : undefined
    }, props, {
      "data-test-automation-id": "day" + day
    }), day);
  };

  _proto.renderMonth = function renderMonth(props, monthIndex) {
    var locale = getLocale(this.props);
    var month = (0, _moment.default)().locale(locale).month(monthIndex);

    var formattedMonth = _lodash.default.capitalize(month.format('MMM').substring(0, 3)); // month name, 3 first letters in upper case


    var label = month.format('MMMM');
    var selected = props.className.indexOf('rdtActive') !== -1;
    var active = !this.props.disabled;
    return _react.default.createElement("td", (0, _extends2.default)({
      role: active ? 'button' : 'presentation',
      tabIndex: active ? 0 : undefined,
      "aria-label": label,
      "aria-selected": selected ? 'true' : undefined
    }, props, {
      "data-test-automation-id": "month" + (monthIndex + 1)
    }), formattedMonth);
  };

  _proto.renderYear = function renderYear(props, year) {
    var selected = props.className.indexOf('rdtActive') !== -1;
    var active = !this.props.disabled;
    return _react.default.createElement("td", (0, _extends2.default)({
      role: active ? 'button' : 'presentation',
      tabIndex: active ? 0 : undefined,
      "aria-label": year,
      "aria-selected": selected ? 'true' : undefined
    }, props, {
      "data-test-automation-id": "year" + year
    }), year);
  };

  _proto.render = function render() {
    var self = this;
    var props = this.getPropsFromContext();
    var hasOnlyDate = false;
    var hasOnlyTime = false;
    var disabled = props.disabled;
    var inputProps = Object.assign({
      disabled: disabled
    }, props.inputProps);
    var timeFormat = props.timeFormat || true;
    var dateFormat = props.dateFormat;

    if (props.hasOnlyDate) {
      hasOnlyDate = !!props.hasOnlyDate;
    } else if (props.hasOnlyTime) {
      hasOnlyTime = !!props.hasOnlyTime;
    }

    if (!props.handsEditable) {
      inputProps = Object.assign({}, inputProps, {
        readOnly: true
      });
    }

    var testAutomationId = props.testAutomationId,
        closeOnTab = props.closeOnTab;
    return _react.default.createElement("div", {
      "data-test-automation-id": testAutomationId,
      className: (0, _classnames.default)('rc-datepicker', {
        'rc-datepicker-disabled': disabled
      })
    }, _react.default.createElement(_Icon.Icon, {
      name: "calendar",
      "aria-hidden": "true"
    }), _react.default.createElement(_BSDatePicker.BSDatePicker, (0, _extends2.default)({}, props, {
      componentClass: "datePicker",
      locale: getLocale(props),
      value: getValue(props),
      ref: "control",
      closeOnTab: closeOnTab,
      closeOnSelect: true,
      inputProps: inputProps,
      timeFormat: !hasOnlyDate && timeFormat,
      dateFormat: dateFormat ? dateFormat : !hasOnlyTime,
      onChange: function onChange(value) {
        if (_lodash.default.isFunction(props.onChange)) {
          props.onChange(getFormatted(props, value));
        }
      },
      renderDay: this.renderDay.bind(this),
      renderMonth: this.renderMonth.bind(this),
      renderYear: this.renderYear.bind(this),
      isValidDate: function isValidDate(currentDate, selectedDate) {
        if (_lodash.default.isFunction(props.isValidDate)) {
          return props.isValidDate(getViewMode(props, self.refs.control), getFormatted(props, currentDate), getFormatted(props, selectedDate));
        }

        return true;
      }
    })));
  };

  return DatePickerControl;
}(_FormControl2.FormControl);

exports.DatePickerControl = DatePickerControl;
(0, _defineProperty2.default)(DatePickerControl, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes, {
  handsEditable: _propTypes.default.bool,
  hasOnlyDate: _propTypes.default.bool,
  hasOnlyTime: _propTypes.default.bool,
  isValidDate: _propTypes.default.func,
  hasStringFormat: _propTypes.default.bool,
  hasTimestampFormat: _propTypes.default.bool,
  hasMomentFormat: _propTypes.default.bool,
  hasDateFormat: _propTypes.default.bool,
  viewMode: _propTypes.default.string,
  locale: _propTypes.default.string,
  testAutomationId: _propTypes.default.string,
  disabled: _propTypes.default.bool,
  closeOnTab: _propTypes.default.bool,
  timeFormat: _propTypes.default.string,
  dateFormat: _propTypes.default.string,
  setPrevMonthText: _propTypes.default.string,
  setNextMonthText: _propTypes.default.string,
  setPrevYearText: _propTypes.default.string,
  setNextYearText: _propTypes.default.string,
  setPrevDecadeText: _propTypes.default.string,
  setNextDecadeText: _propTypes.default.string,
  changeMonthText: _propTypes.default.string,
  changeYearText: _propTypes.default.string,
  setNextHourText: _propTypes.default.string,
  setPrevHourText: _propTypes.default.string,
  setNextMinuteText: _propTypes.default.string,
  setPrevMinuteText: _propTypes.default.string,
  changePMAMText: _propTypes.default.string
}));
(0, _defineProperty2.default)(DatePickerControl, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  handsEditable: false,
  hasOnlyDate: false,
  hasOnlyTime: false,
  isValidDate: null,
  hasStringFormat: false,
  hasTimestampFormat: false,
  hasMomentFormat: false,
  hasDateFormat: true,
  viewMode: 'days',
  locale: 'en_US',
  testAutomationId: 'controlDatePicker',
  disabled: false,
  closeOnTab: false,
  setPrevMonthText: 'TRANSLATE:SET_PREV_MONTH',
  setNextMonthText: 'TRANSLATE:SET_NEXT_MONTH',
  setPrevYearText: 'TRANSLATE:SET_PREV_YEAR',
  setNextYearText: 'TRANSLATE:SET_NEXT_YEAR',
  setPrevDecadeText: 'TRANSLATE:SET_PREV_DECADE',
  setNextDecadeText: 'TRANSLATE:SET_NEXT_DECADE',
  changeMonthText: 'TRANSLATE:CHANGE_MONTH',
  changeYearText: 'TRANSLATE:CHANGE_YEAR',
  setPrevHourText: 'TRANSLATE:SET_PREV_HOUR',
  setNextHourText: 'TRANSLATE:SET_NEXT_HOUR',
  setPrevMinuteText: 'TRANSLATE:SET_PREV_MINUTE',
  setNextMinuteText: 'TRANSLATE:SET_NEXT_MINUTE',
  toggleAMPMText: 'TRANSLATE:TOGGLE_AM_PM',
  dateFormat: null
}));

function getValue(props) {
  var value = props.value;

  if (!value) {
    return null;
  }

  if (typeof value === 'number') {
    return new Date(props.value);
  }

  if (typeof value === 'string' && (0, _moment.default)(value).isValid()) {
    return (0, _moment.default)(value).toDate();
  }

  if (value instanceof Date) {
    return value;
  }

  return value.toDate();
}

function getLocale(props) {
  return props.locale || (0, _utils.getConfig)().userLanguage || 'en_US';
}

function getFormatted(props, value) {
  var hasStringFormat = props.hasStringFormat;
  var hasTimestampFormat = props.hasStringFormat;
  var hasMomentFormat = props.hasMomentFormat;

  if (!value) {
    return null;
  }

  if (typeof value === 'string') {
    return value;
  }

  if (hasStringFormat) {
    return value.format();
  }

  if (hasTimestampFormat) {
    return Number(value.toDate());
  }

  if (hasMomentFormat) {
    return value;
  }

  return value.toDate();
}

function getViewMode(props, control) {
  if (control) {
    // todo: this lucid hardcode otherwise not working
    // todo: this temporary implementation and in the future this component should be re-implemented w/o hardcode
    return control.state.currentView || 'days';
  }

  return props.viewMode || 'days';
}