'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { FormControl } from './FormControl';
import _ from 'lodash';
import { BSDatePicker } from './BSDatePicker';
import moment from 'moment';
import { getConfig } from '../../../utils';
import classnames from 'classnames';
import { Icon } from '../Icon';
export var DatePickerControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(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.createElement("td", _extends({
      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 = moment().locale(locale).month(monthIndex);

    var formattedMonth = _.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.createElement("td", _extends({
      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.createElement("td", _extends({
      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.createElement("div", {
      "data-test-automation-id": testAutomationId,
      className: classnames('rc-datepicker', {
        'rc-datepicker-disabled': disabled
      })
    }, React.createElement(Icon, {
      name: "calendar",
      "aria-hidden": "true"
    }), React.createElement(BSDatePicker, _extends({}, 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 (_.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 (_.isFunction(props.isValidDate)) {
          return props.isValidDate(getViewMode(props, self.refs.control), getFormatted(props, currentDate), getFormatted(props, selectedDate));
        }

        return true;
      }
    })));
  };

  return DatePickerControl;
}(FormControl);

_defineProperty(DatePickerControl, "propTypes", Object.assign({}, FormControl.propTypes, {
  handsEditable: PropTypes.bool,
  hasOnlyDate: PropTypes.bool,
  hasOnlyTime: PropTypes.bool,
  isValidDate: PropTypes.func,
  hasStringFormat: PropTypes.bool,
  hasTimestampFormat: PropTypes.bool,
  hasMomentFormat: PropTypes.bool,
  hasDateFormat: PropTypes.bool,
  viewMode: PropTypes.string,
  locale: PropTypes.string,
  testAutomationId: PropTypes.string,
  disabled: PropTypes.bool,
  closeOnTab: PropTypes.bool,
  timeFormat: PropTypes.string,
  dateFormat: PropTypes.string,
  setPrevMonthText: PropTypes.string,
  setNextMonthText: PropTypes.string,
  setPrevYearText: PropTypes.string,
  setNextYearText: PropTypes.string,
  setPrevDecadeText: PropTypes.string,
  setNextDecadeText: PropTypes.string,
  changeMonthText: PropTypes.string,
  changeYearText: PropTypes.string,
  setNextHourText: PropTypes.string,
  setPrevHourText: PropTypes.string,
  setNextMinuteText: PropTypes.string,
  setPrevMinuteText: PropTypes.string,
  changePMAMText: PropTypes.string
}));

_defineProperty(DatePickerControl, "defaultProps", Object.assign({}, 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' && moment(value).isValid()) {
    return moment(value).toDate();
  }

  if (value instanceof Date) {
    return value;
  }

  return value.toDate();
}

function getLocale(props) {
  return props.locale || 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';
}