"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "DatePickerBase", {
  enumerable: true,
  get: function get() {
    return _DatePickerBase["default"];
  }
});
exports.dateToLocaleString = exports.DatePickerDecorator = exports.DatePicker = exports["default"] = void 0;

var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));

var _DateFactory = _interopRequireDefault(require("ilib/lib/DateFactory"));

var _DateFmt = _interopRequireDefault(require("ilib/lib/DateFmt"));

var _compose = _interopRequireDefault(require("ramda/src/compose"));

var _DateTime = require("../internal/DateTime");

var _Skinnable = _interopRequireDefault(require("../Skinnable"));

var _DatePickerBase = _interopRequireDefault(require("./DatePickerBase"));

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

/**
 * Date selection components and behaviors.
 *
 * @example
 * <DatePicker onChange={console.log} />
 *
 * @module agate/DatePicker
 * @exports DatePicker
 * @exports DatePickerBase
 * @exports DatePickerDecorator
 * @exports dateToLocaleString
 */
var getLabelFormatter = function getLabelFormatter() {
  return new _DateFmt["default"]({
    date: 'dmwy',
    length: 'full',
    timezone: 'local',
    useNative: false
  });
};

var dateTimeConfig = {
  customProps: function customProps(i18n, value, dayReverseTransition, props) {
    var values = {
      maxMonths: 12,
      maxDays: 31,
      year: 1900,
      month: 1,
      day: 1,
      dayReverseTransition: false
    };

    if (value && i18n) {
      values.year = value.getYears();
      values.month = value.getMonths();
      values.day = value.getDays();
      values.maxMonths = i18n.formatter.cal.getNumMonths(values.year);
      values.maxDays = i18n.formatter.cal.getMonLength(values.month, values.year);
      values.maxYear = i18n.toLocalYear(props.maxYear || _DatePickerBase["default"].defaultProps.maxYear);
      values.minYear = i18n.toLocalYear(props.minYear || _DatePickerBase["default"].defaultProps.minYear);
    }

    if (dayReverseTransition) {
      values.dayReverseTransition = dayReverseTransition;
    }

    return values;
  },
  defaultOrder: ['d', 'm', 'y'],
  handlers: {
    onDateChange: function onDateChange(ev, value) {
      value.day = ev.value;
      return value;
    },
    onMonthChange: function onMonthChange(ev, value) {
      value.month = ev.value;
      return value;
    },
    onYearChange: function onYearChange(ev, value) {
      value.year = ev.value;
      return value;
    }
  },
  i18n: function i18n() {
    var order = getLabelFormatter().getTemplate().replace(/'.*?'/g, '').match(/([mdy]+)/ig).map(function (s) {
      return s[0].toLowerCase();
    });
    /*
     * Converts a gregorian year to local year
     *
     * @param	{Number}	year	gregorian year
     *
     * @returns	{Number}		local year
     */

    var toLocalYear = function toLocalYear(year) {
      return (0, _DateFactory["default"])({
        julianday: (0, _DateFactory["default"])({
          year: year,
          type: 'gregorian',
          month: 1,
          day: 1,
          timezone: 'local'
        }).getJulianDay(),
        timezone: 'local'
      }).getYears();
    };

    return {
      formatter: getLabelFormatter(),
      order: order,
      toLocalYear: toLocalYear
    };
  }
};
/**
 * Applies Agate specific behaviors to [DatePickerBase]{@link agate/DatePicker.DatePickerBase} components.
 *
 * @hoc
 * @memberof agate/DatePicker
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

var DatePickerDecorator = (0, _compose["default"])(_Pure["default"], _Skinnable["default"]);
/**
 * A date selection component, ready to use in Agate applications.
 *
 * `DatePicker` may be used to select the year, month, and day. It uses a standard `Date` object for
 * its `value` which can be shared as the `value` for a
 * [TimePicker]{@link agate/TimePicker.TimePicker} to select both a date and time.
 *
 * By default, `DatePicker` maintains the state of its `value` property. Supply the
 * `defaultValue` property to control its initial value. If you wish to directly control updates
 * to the component, supply a value to `value` at creation time and update it in response to
 * `onChange` events.
 *
 * Usage:
 * ```
 * <DatePicker
 *   defaultValue={selectedDate}
 *   onChange={handleChange}
 * />
 * ```
 *
 * @class DatePicker
 * @memberof agate/DatePicker
 * @extends agate/DatePicker.DatePickerBase
 * @mixes agate/DatePicker.DatePickerDecorator
 * @omit day
 * @omit maxDays
 * @omit maxMonths
 * @omit month
 * @omit order
 * @omit year
 * @ui
 * @public
 */

exports.DatePickerDecorator = DatePickerDecorator;
var DatePicker = DatePickerDecorator((0, _DateTime.DateTimeDecorator)(dateTimeConfig, _DatePickerBase["default"]));
/**
 * The initial value used when `value` is not set.
 *
 * @name defaultValue
 * @type {Date}
 * @memberof agate/DatePicker.DatePicker.prototype
 * @public
 */

/**
 * The selected date
 *
 * @name value
 * @type {Date}
 * @memberof agate/DatePicker.DatePicker.prototype
 * @public
 */

/**
 * Converts a standard `Date` object into a locale-specific string.
 *
 * @function
 * @memberof agate/DatePicker
 * @param {Date} date `Date` to convert
 * @returns {String?} Converted date or `null` if `date` is invalid
 */

exports.DatePicker = DatePicker;

var dateToLocaleString = function dateToLocaleString(date) {
  if (!date) {
    return null;
  }

  return getLabelFormatter().format(date);
};

exports.dateToLocaleString = dateToLocaleString;
var _default = DatePicker;
exports["default"] = _default;