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 __());
    };
})();
import { default as BSDateTimeControl } from 'react-datetime';
import ReactDOM from 'react-dom';
import _ from 'lodash';
var ENTER_KEY_CODE = 13;
var SPACE_KEY_CODE = 32;
var ESC_KEY_CODE = 27;
var VIEWS = {
    DAYS: 'days',
    MONTHS: 'months',
    YEARS: 'years',
    TIME: 'time'
};
var blurSelector = '[tabindex="0"], .rdt > input';
/**
 * @param {string} eventName
 * @param {{[bubbles]: Boolean, [cancelable]: Boolean}} eventArgs
 * */
var createMouseEvent = function (eventName, eventArgs) {
    if (eventArgs === void 0) { eventArgs = {}; }
    try {
        return new MouseEvent(eventName, eventArgs);
    }
    catch (e) {
        // https://jira.ringcentral.com/browse/UIA-68403
        var event_1 = document.createEvent('MouseEvent');
        event_1.initMouseEvent(eventName, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        return event_1;
    }
};
var BSDatePicker = /** @class */ (function (_super) {
    __extends(BSDatePicker, _super);
    function BSDatePicker() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.onDatePickerClick = function () {
            // Prevent close date picker by closeUnfocused method
            _this.stopClosing = true;
            setTimeout(function () {
                _this.stopClosing = false;
            }, 100);
        };
        _this.closeUnfocused = function () {
            // Wait for changing a focus from input to popover and back
            setTimeout(function () {
                if (_this.stopClosing) {
                    _this.stopClosing = false;
                    return;
                }
                if (!_this.isFocused()) {
                    _this.closeCalendar();
                }
            }, 50);
        };
        _this.focusInput = function () {
            var node = ReactDOM.findDOMNode(_this);
            var input = node.querySelector('.rdt > input.form-control');
            if (input) {
                input.focus();
            }
        };
        _this.mouseDownByKeyboard = function (event) {
            _this.closeUnfocused();
            if (event.keyCode === ESC_KEY_CODE) {
                event.stopPropagation();
                _this.focusInput();
                _this.closeCalendar();
            }
            if (_this.props['disabled'] || event.target.isMouseDownByKeyboard) {
                return;
            }
            if (event.keyCode === ENTER_KEY_CODE || event.keyCode === SPACE_KEY_CODE) {
                event.target.dispatchEvent(createMouseEvent('mousedown', { bubbles: true }));
                event.target.isMouseDownByKeyboard = true;
            }
        };
        _this.mouseUpByKeyboard = function (event) {
            if (_this.props['disabled']) {
                return;
            }
            if (event.keyCode === ESC_KEY_CODE) {
                event.stopPropagation();
            }
            if (event.keyCode === ENTER_KEY_CODE || event.keyCode === SPACE_KEY_CODE) {
                event.target.isMouseDownByKeyboard = false;
                event.target.dispatchEvent(createMouseEvent('mouseup', { bubbles: true }));
            }
        };
        _this.clickByKeyboard = function (event) {
            if (_this.props['disabled']) {
                return;
            }
            if (event.keyCode === ENTER_KEY_CODE || event.keyCode === SPACE_KEY_CODE) {
                event.target.click();
            }
        };
        return _this;
    }
    BSDatePicker.prototype.componentDidMount = function () {
        this.adjustForRC();
        this.addEventListeners();
    };
    BSDatePicker.prototype.componentDidUpdate = function () {
        this.adjustForRC();
    };
    BSDatePicker.prototype.componentWillUnmount = function () {
        this.removeEventListeners();
    };
    BSDatePicker.prototype.adjustForRC = function () {
        var _this = this;
        var _a = this.props, disabled = _a.disabled, changeYearText = _a.changeYearText, changeMonthText = _a.changeMonthText, setPrevDecadeText = _a.setPrevDecadeText, setNextDecadeText = _a.setNextDecadeText, setPrevYearText = _a.setPrevYearText, setNextYearText = _a.setNextYearText, setPrevMonthText = _a.setPrevMonthText, setNextMonthText = _a.setNextMonthText, setPrevHourText = _a.setPrevHourText, setNextHourText = _a.setNextHourText, setPrevMinuteText = _a.setPrevMinuteText, setNextMinuteText = _a.setNextMinuteText, toggleAMPMText = _a.toggleAMPMText;
        var currentView = this.state.currentView;
        var setAutomationId = function (node, testId) {
            if (node) {
                node.setAttribute('data-test-automation-id', testId);
            }
        };
        var setKeyboardInteractive = function (node, interactive) {
            if (interactive === void 0) { interactive = true; }
            if (node) {
                if (interactive && !disabled) {
                    node.setAttribute('role', 'button');
                    node.setAttribute('tabindex', 0);
                    node.addEventListener('keydown', _this.mouseDownByKeyboard);
                }
                else {
                    node.removeAttribute('role');
                    node.removeAttribute('tabindex');
                }
            }
        };
        var setAriaLabel = function (node, ariaLabel) {
            if (node) {
                if (ariaLabel) {
                    node.setAttribute('aria-label', ariaLabel);
                }
                else {
                    node.removeAttribute('aria-label');
                }
            }
        };
        var node = ReactDOM.findDOMNode(this);
        var previousButton = node.querySelector('.rdtPrev');
        var nextButton = node.querySelector('.rdtNext');
        var switchViewButton = node.querySelector('.rdtSwitch');
        var switchToTimeViewButton = node.querySelector('.rdtTimeToggle');
        var _b = _.toArray(node.querySelectorAll(".rdtCounters .rdtBtn")), hoursUp = _b[0], hoursDown = _b[1], minutesUp = _b[2], minutesDown = _b[3], AMPMUp = _b[4], AMPMDown = _b[5];
        var _c = _.toArray(node.querySelectorAll('.rdtCounters .rdtCount')), hour = _c[0], minute = _c[1], ampm = _c[2];
        setAutomationId(node.querySelector('.rdtPicker'), 'datePickerOverlay');
        setAutomationId(previousButton, 'buttonPrev');
        setAutomationId(nextButton, 'buttonNext');
        setAutomationId(switchViewButton, 'buttonSwitch');
        setAutomationId(switchToTimeViewButton, 'buttonSwitchTime');
        setAutomationId(hoursUp, 'buttonUpHour');
        setAutomationId(hoursDown, 'buttonDownHour');
        setAutomationId(minutesUp, 'buttonUpMinute');
        setAutomationId(minutesDown, 'buttonDownMinute');
        setAutomationId(AMPMUp, 'buttonUpPMAM');
        setAutomationId(AMPMDown, 'buttonDownPMAM');
        setAutomationId(hour, 'currentHour');
        setAutomationId(minute, 'currentMinute');
        setAutomationId(ampm, 'currentPMAM');
        setKeyboardInteractive(previousButton);
        setKeyboardInteractive(nextButton);
        setKeyboardInteractive(switchViewButton, currentView !== VIEWS.YEARS);
        setKeyboardInteractive(switchToTimeViewButton);
        setKeyboardInteractive(hoursUp);
        setKeyboardInteractive(hoursDown);
        setKeyboardInteractive(minutesUp);
        setKeyboardInteractive(minutesDown);
        setKeyboardInteractive(AMPMUp);
        setKeyboardInteractive(AMPMDown);
        switch (currentView) {
            case VIEWS.DAYS:
                if (switchViewButton) {
                    setAriaLabel(switchViewButton, switchViewButton.innerText + ", " + changeMonthText);
                }
                setAriaLabel(previousButton, setPrevMonthText);
                setAriaLabel(nextButton, setNextMonthText);
                break;
            case VIEWS.MONTHS:
                if (switchViewButton) {
                    setAriaLabel(switchViewButton, switchViewButton.innerText + ", " + changeYearText);
                }
                setAriaLabel(previousButton, setPrevYearText);
                setAriaLabel(nextButton, setNextYearText);
                break;
            case VIEWS.YEARS:
                if (switchViewButton) {
                    setAriaLabel(switchViewButton, switchViewButton.innerText);
                }
                setAriaLabel(previousButton, setPrevDecadeText);
                setAriaLabel(nextButton, setNextDecadeText);
                break;
            case VIEWS.TIME:
                setAriaLabel(hoursUp, setPrevHourText);
                setAriaLabel(hoursDown, setNextHourText);
                setAriaLabel(minutesUp, setPrevMinuteText);
                setAriaLabel(minutesDown, setNextMinuteText);
                setAriaLabel(AMPMUp, toggleAMPMText);
                setAriaLabel(AMPMDown, toggleAMPMText);
                break;
        }
        if (this.adjustedView && this.adjustedView !== currentView) {
            if (currentView === VIEWS.TIME) {
                hoursUp.focus();
            }
            else {
                var focusNode = node.querySelector('.rdtPicker .rdtActive') ||
                    node.querySelector('.rdtDay[tabindex="0"]') ||
                    node.querySelector('.rdtMonth[tabindex="0"]') ||
                    node.querySelector('.rdtYear[tabindex="0"]');
                if (focusNode) {
                    focusNode.focus();
                }
            }
        }
        this.adjustedView = currentView;
        if (minutesDown) {
            minutesDown.addEventListener('keydown', this.closeUnfocused);
        }
    };
    BSDatePicker.prototype.addEventListeners = function () {
        var _this = this;
        var node = ReactDOM.findDOMNode(this);
        if (node) {
            node.addEventListener('mousedown', this.onDatePickerClick);
            node.addEventListener('keydown', this.mouseDownByKeyboard);
            node.addEventListener('keyup', this.mouseUpByKeyboard);
            node.addEventListener('keypress', this.clickByKeyboard);
            _.toArray(node.querySelectorAll(blurSelector)).forEach(function (item) {
                item.addEventListener('blur', _this.closeUnfocused);
            });
        }
    };
    BSDatePicker.prototype.removeEventListeners = function () {
        var _this = this;
        var node = ReactDOM.findDOMNode(this);
        if (node) {
            node.removeEventListener('mousedown', this.onDatePickerClick);
            node.removeEventListener('keydown', this.mouseDownByKeyboard);
            node.removeEventListener('keyup', this.mouseUpByKeyboard);
            node.removeEventListener('keypress', this.clickByKeyboard);
            _.toArray(node.querySelectorAll(blurSelector)).forEach(function (item) {
                item.removeEventListener('blur', _this.closeUnfocused);
            });
        }
    };
    BSDatePicker.prototype.isFocused = function () {
        var node = ReactDOM.findDOMNode(this);
        return !!(node && node.querySelector(':focus'));
    };
    return BSDatePicker;
}(BSDateTimeControl));
export { BSDatePicker };
//# sourceMappingURL=BSDatePicker.js.map