"use strict";
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 __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssClass = void 0;
var classNames = require("classnames");
var React = require("react");
var react_1 = require("react");
var downshift_1 = require("downshift");
var FontAwesome = require("react-fontawesome");
var flex_1 = require("../flex");
var Forms_1 = require("../utils/Forms");
require("./Select2.less");
exports.cssClass = {
    CONTAINER: "Select2--container",
    INFO_ROW: "Select2--infoRow",
    INFO_ROW_LABEL_HIDDEN: "Select2--infoRow--labelHidden",
    INFO_REQUIREMENT: "Select2--infoRequirement",
    LABEL: "Select2--label",
    LABEL_HIDDEN: "Select2--label--hidden",
    SELECT_CONTAINER: "Select2--selectContainer",
    SELECT_CONTAINER_FOCUSED: "Select2--selectContainer--focused",
    SELECT_CONTAINER_ERROR: "Select2--selectContainer--error",
    SELECT_CONTAINER_DISABLED: "Select2--selectContainer--disabled",
    INPUT: "Select2--input",
    TRAILING_ELEMENT: "Select2--trailingElement",
    ERROR_ICON: "Select2--errorIcon",
    BUTTON_RESET: "Select2--button--reset",
    CLEAR_BUTTON: "Select2--clearButton",
    CARET_BUTTON: "Select2--caretButton",
    MENU_RESET: "Select2--options--reset",
    MENU_OPEN: "Select2--options--open",
    MENU_OPTION: "Select2--option",
    MENU_OPTION_HIGHLIGHTED: "Select2--option--highlighted",
    NO_OPTIONS_FOUND: "Select2--notFound",
};
/*
  Basic single-item-select drop down component
*/
var Select2 = function (_a) {
    var className = _a.className, name = _a.name, label = _a.label, hideLabel = _a.hideLabel, initialOptions = _a.options, clearable = _a.clearable, creatable = _a.creatable, requirement = _a.requirement, initialIsInError = _a.initialIsInError, value = _a.value, onChange = _a.onChange, size = _a.size, placeholder = _a.placeholder;
    var _b = react_1.useState(null), createdOption = _b[0], setCreatedOption = _b[1];
    var options = createdOption ? __spreadArrays(initialOptions, [createdOption]) : initialOptions;
    var optionValues = new Set(options.map(function (o) { return o.value; }));
    var _c = react_1.useState(initialOptions), selectableOptions = _c[0], setSelectableOptions = _c[1];
    react_1.useEffect(function () {
        // We need this effect in order to get the state to update on prop change
        // in re-renders, otherwise options may be outdated
        setSelectableOptions(initialOptions);
    }, [initialOptions]);
    var _d = react_1.useState(false), isCreating = _d[0], setIsCreating = _d[1];
    // handle initial error state via focus state since
    // hooks can't combine the downshift state well with component hook state
    var _e = react_1.useState(false), hasBeenFocused = _e[0], setHasBeenFocused = _e[1];
    react_1.useEffect(function () {
        setHasBeenFocused(false);
    }, [initialIsInError]);
    var _f = downshift_1.useCombobox({
        items: selectableOptions,
        itemToString: function (o) { return (o ? o.label : ""); },
        selectedItem: options.find(function (o) { return o.value === value; }) || null,
        stateReducer: function (state, actionAndChanges) {
            var changes = actionAndChanges.changes, type = actionAndChanges.type;
            switch (type) {
                case downshift_1.useCombobox.stateChangeTypes.InputKeyDownEscape:
                    return __assign(__assign({}, changes), { selectedItem: null, inputValue: "", isOpen: true });
                case downshift_1.useCombobox.stateChangeTypes.InputBlur: {
                    // reset any text that has been entered
                    return __assign(__assign({}, changes), { selectedItem: selectedItem, inputValue: selectedItem ? selectedItem.label : "" });
                }
                default:
                    break;
            }
            return changes;
        },
        onIsOpenChange: function (changes) {
            var type = changes.type;
            switch (type) {
                // if we're opening the drop down for the first time
                // show everything even if something had already been selected previously
                case downshift_1.useCombobox.stateChangeTypes.FunctionOpenMenu:
                    setSelectableOptions(options);
                    break;
                default:
                    break;
            }
            return;
        },
        onInputValueChange: function (change) {
            var inputLowerCase = change.inputValue.toLowerCase();
            var filteredOptions = options.filter(function (o) { return o.label.toLowerCase().includes(inputLowerCase); });
            if (isCreating && filteredOptions.length > 0) {
                setIsCreating(false);
            }
            setSelectableOptions(filteredOptions);
        },
        onSelectedItemChange: function (item) {
            if (creatable) {
                if (optionValues.has(item.selectedItem.value)) {
                    setSelectableOptions(initialOptions);
                    setCreatedOption(null);
                }
                else {
                    setSelectableOptions(__spreadArrays(initialOptions, [item.selectedItem]));
                    setCreatedOption(item.selectedItem);
                    setIsCreating(false);
                }
            }
            if (onChange) {
                onChange(item.selectedItem ? item.selectedItem.value : "");
            }
        },
    }), isOpen = _f.isOpen, getToggleButtonProps = _f.getToggleButtonProps, getLabelProps = _f.getLabelProps, getMenuProps = _f.getMenuProps, getInputProps = _f.getInputProps, getComboboxProps = _f.getComboboxProps, highlightedIndex = _f.highlightedIndex, getItemProps = _f.getItemProps, openMenu = _f.openMenu, selectedItem = _f.selectedItem;
    var inputVal = getInputProps().value;
    react_1.useEffect(function () {
        if (creatable && selectableOptions.length === 0 && inputVal.length > 0) {
            setIsCreating(true);
            setSelectableOptions([{ label: "" + inputVal, value: inputVal }]);
        }
    }, [creatable, selectableOptions, setIsCreating, setSelectableOptions, inputVal]);
    var isInError = requirement === Forms_1.FormElementRequirement.REQUIRED &&
        (hasBeenFocused ? !selectedItem : initialIsInError);
    var id = name;
    var inputRef = react_1.useRef();
    return (React.createElement("div", { className: classNames(exports.cssClass.CONTAINER, Forms_1.formElementSizeClassName(size), className) },
        React.createElement("div", { className: classNames(exports.cssClass.INFO_ROW, hideLabel && exports.cssClass.INFO_ROW_LABEL_HIDDEN) },
            React.createElement("label", __assign({ className: classNames(exports.cssClass.LABEL, hideLabel && exports.cssClass.LABEL_HIDDEN) }, getLabelProps()), label),
            requirement && (React.createElement("label", __assign({ className: exports.cssClass.INFO_REQUIREMENT, "aria-live": "polite" }, getLabelProps()), requirement))),
        React.createElement("div", __assign({ className: classNames(exports.cssClass.SELECT_CONTAINER, isOpen && exports.cssClass.SELECT_CONTAINER_FOCUSED, isInError && exports.cssClass.SELECT_CONTAINER_ERROR, requirement === Forms_1.FormElementRequirement.DISABLED && exports.cssClass.SELECT_CONTAINER_DISABLED) }, getComboboxProps({
            onClick: function (e) {
                if (requirement === Forms_1.FormElementRequirement.DISABLED) {
                    e.stopPropagation();
                    return;
                }
                if (!isOpen) {
                    openMenu();
                    if (inputRef.current) {
                        inputRef.current.select();
                    }
                }
            },
        })),
            React.createElement("input", __assign({ id: id, name: id, placeholder: placeholder, className: exports.cssClass.INPUT }, getInputProps({
                ref: inputRef,
                onFocus: function () { return setHasBeenFocused(true); },
                disabled: requirement === Forms_1.FormElementRequirement.DISABLED,
            }))),
            isInError && (React.createElement("div", { className: classNames(exports.cssClass.TRAILING_ELEMENT, exports.cssClass.ERROR_ICON) },
                React.createElement(FontAwesome, { name: "exclamation-circle" }))),
            clearable && selectedItem && (React.createElement(flex_1.FlexBox, { justify: "center", alignItems: "center" },
                React.createElement("button", { className: classNames(exports.cssClass.BUTTON_RESET, exports.cssClass.TRAILING_ELEMENT, exports.cssClass.CLEAR_BUTTON), "aria-label": "clear", onClick: function (e) {
                        e.stopPropagation();
                        if (requirement === Forms_1.FormElementRequirement.DISABLED) {
                            return;
                        }
                        onChange(null);
                        openMenu();
                        if (inputRef.current) {
                            inputRef.current.select();
                        }
                    } }, "\u2715"))),
            React.createElement("button", __assign({ className: classNames(exports.cssClass.BUTTON_RESET, exports.cssClass.TRAILING_ELEMENT, exports.cssClass.CARET_BUTTON) }, getToggleButtonProps({ disabled: requirement === Forms_1.FormElementRequirement.DISABLED })),
                React.createElement(FontAwesome, { name: isOpen ? "caret-up" : "caret-down" }))),
        React.createElement("ul", __assign({ className: classNames(exports.cssClass.MENU_RESET, isOpen && exports.cssClass.MENU_OPEN) }, getMenuProps()), isOpen &&
            (selectableOptions.length > 0 ? (selectableOptions.map(function (o, i) { return (React.createElement("li", __assign({ className: classNames(exports.cssClass.MENU_OPTION, i === highlightedIndex && exports.cssClass.MENU_OPTION_HIGHLIGHTED), key: "" + o.value + i }, getItemProps({ item: o, index: i })), isCreating ? "Add \"" + o.label + "\"" : o.customLabel || o.label)); })) : (React.createElement("li", { className: classNames(exports.cssClass.NO_OPTIONS_FOUND) }, "No options"))))));
};
exports.default = Select2;
