"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var lodash_1 = __importDefault(require("lodash"));
var prop_types_1 = __importDefault(require("prop-types"));
var Block_1 = require("../../Block");
var TimeNumberEditor_1 = require("./TimeNumberEditor");
var Row_1 = require("../../Row");
var Col_1 = require("../../Col");
var TimeAmPmEditor_1 = require("./TimeAmPmEditor");
var instanceCounter = 0;
var TimePicker = /** @class */ (function (_super) {
    __extends(TimePicker, _super);
    function TimePicker(props, ctx) {
        var _this = _super.call(this, props, ctx) || this;
        _this.getMaxHour = function () {
            return _this.props.is24hour ? 23 : 11;
        };
        _this.onTimeEditorChange = function (updates) {
            var _a = _this.props, onChange = _a.onChange, value = _a.value;
            onChange(__assign({}, value, updates));
        };
        _this.onHoursChanges = function (hours) {
            _this.onTimeEditorChange({ hours: hours });
        };
        _this.handleChangeMinutes = function (minutes) {
            if (minutes === -1) {
                minutes = 59;
            }
            if (minutes === 60) {
                minutes = 0;
            }
            _this.onTimeEditorChange({ minutes: minutes });
        };
        _this.handleChangeAmPm = function (isSwitchToAM) {
            var isCurrentAmMode = _this.isAmMode();
            var isModeChanged = isSwitchToAM === !isCurrentAmMode;
            var value = _this.props.value;
            var oldHours = value.hours;
            var newHours = null;
            if (isModeChanged) {
                if (oldHours !== 12) {
                    if (isSwitchToAM) {
                        newHours = oldHours - 12;
                    }
                    else {
                        newHours = oldHours + 12;
                    }
                }
                else {
                    newHours = 0;
                }
            }
            if (newHours !== null) {
                _this.onHoursChanges(newHours);
            }
        };
        _this.handleChangeHours = function (newHours) {
            var hours = newHours;
            if (!_this.props.is24hour) {
                if (_this.isAmMode()) {
                    if (hours > 11) {
                        hours = 0;
                    }
                    if (hours < 0) {
                        hours = 11;
                    }
                }
                else {
                    hours = newHours + 12;
                    if (hours === 24) {
                        hours = 12;
                    }
                    if (hours > 24) {
                        hours = 13;
                    }
                }
            }
            _this.onHoursChanges(hours);
        };
        _this.handleStopPropagation = function (e) { return e.stopPropagation(); };
        _this.handleBackToCalendar = function (e) { return _this.props.onBackToCalendar(); };
        _this.instanceId = ++instanceCounter;
        return _this;
    }
    TimePicker.prototype.isAmMode = function () {
        var value = this.props.value;
        var hours = value.hours;
        return hours < 12;
    };
    TimePicker.prototype.renderAmPMSwitcher = function () {
        var _a = this.props, amAriaLabel = _a.amAriaLabel, pmAriaLabel = _a.pmAriaLabel, enableAMTitle = _a.enableAMTitle, enablePMTitle = _a.enablePMTitle, amPmSwitcherLabel = _a.amPmSwitcherLabel;
        var isAm = this.isAmMode();
        return (react_1.default.createElement(TimeAmPmEditor_1.TimeAmPmEditor, { isAm: isAm, onAmPmChange: this.handleChangeAmPm, amLabel: amAriaLabel, pmLabel: pmAriaLabel, amPmSwitcherLabel: amPmSwitcherLabel, enableAMTitle: enableAMTitle, enablePMTitle: enablePMTitle }));
    };
    TimePicker.prototype.getHoursDisplayValue = function () {
        var _a = this.props, value = _a.value, is24hour = _a.is24hour;
        if (is24hour || this.isAmMode() || value.hours === 12) {
            return value.hours;
        }
        return value.hours - 12;
    };
    TimePicker.prototype.renderTimeColon = function () {
        return (react_1.default.createElement("div", { className: "rc-datepicker-colon" },
            react_1.default.createElement("div", { className: "time-text text-center" }, ":")));
    };
    TimePicker.prototype.render = function () {
        var _a = this.props, value = _a.value, onPanelFocus = _a.onPanelFocus, is24hour = _a.is24hour, ariaDescription = _a.ariaDescription, hourIncreaseBtnAriaLabel = _a.hourIncreaseBtnAriaLabel, hourDecreaseBtnAriaLabel = _a.hourDecreaseBtnAriaLabel, minuteDecreaseBtnAriaLabel = _a.minuteDecreaseBtnAriaLabel, minuteIncreaseBtnAriaLabel = _a.minuteIncreaseBtnAriaLabel;
        var descriptionId = "time-picker-description-" + this.instanceId;
        var role = 'group';
        return (react_1.default.createElement(Block_1.Block, { className: "rc-datepicker-time-editor", addHorizontalOffset: true, addTopOffset: true, onKeyUp: this.handleStopPropagation, onFocus: onPanelFocus, role: role, "aria-describedby": descriptionId },
            react_1.default.createElement("p", { className: "sr-only", id: descriptionId }, ariaDescription),
            react_1.default.createElement(Row_1.Row, null,
                react_1.default.createElement(Col_1.Col, { sm: 3, md: 3, xs: 3 },
                    react_1.default.createElement(TimeNumberEditor_1.TimeNumberEditor, { testAutomationId: "hoursEditor", decreaseButtonLabel: hourDecreaseBtnAriaLabel, increaseButtonLabel: hourIncreaseBtnAriaLabel, onShiftTabAwait: this.handleBackToCalendar, value: this.getHoursDisplayValue(), onChange: this.handleChangeHours, max: this.getMaxHour(), min: 0 })),
                react_1.default.createElement(Col_1.Col, { sm: 1, md: 1, xs: 1 }, this.renderTimeColon()),
                react_1.default.createElement(Col_1.Col, { sm: 3, md: 3, xs: 3 },
                    react_1.default.createElement(TimeNumberEditor_1.TimeNumberEditor, { testAutomationId: "minutesEditor", increaseButtonLabel: minuteIncreaseBtnAriaLabel, decreaseButtonLabel: minuteDecreaseBtnAriaLabel, value: value.minutes, onChange: this.handleChangeMinutes, valueDisplayFormat: function (value) { return lodash_1.default.padStart(value, 2, '0'); }, max: 11, min: 0 })),
                react_1.default.createElement(Col_1.Col, { sm: 3, md: 3, xs: 3 }, is24hour ? null : this.renderAmPMSwitcher()))));
    };
    TimePicker.propTypes = {
        value: prop_types_1.default.shape({
            hours: prop_types_1.default.number,
            minutes: prop_types_1.default.number
        }),
        onChange: prop_types_1.default.func.isRequired,
        is24hour: prop_types_1.default.bool,
        onPanelFocus: prop_types_1.default.func.isRequired,
        onBackToCalendar: prop_types_1.default.func.isRequired,
        ariaDescription: prop_types_1.default.string.isRequired,
        hourIncreaseBtnAriaLabel: prop_types_1.default.string.isRequired,
        hourDecreaseBtnAriaLabel: prop_types_1.default.string.isRequired,
        minuteIncreaseBtnAriaLabel: prop_types_1.default.string.isRequired,
        minuteDecreaseBtnAriaLabel: prop_types_1.default.string.isRequired,
        amAriaLabel: prop_types_1.default.string.isRequired,
        pmAriaLabel: prop_types_1.default.string.isRequired,
        amPmSwitcherLabel: prop_types_1.default.string.isRequired,
        enablePMTitle: prop_types_1.default.string.isRequired,
        enableAMTitle: prop_types_1.default.string.isRequired
    };
    TimePicker.defaultProps = {
        value: {
            hours: 0,
            minutes: 0
        },
        is24hour: true
    };
    return TimePicker;
}(react_1.default.PureComponent));
exports.TimePicker = TimePicker;
//# sourceMappingURL=TimePicker.js.map