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);
};
import * as React from "react";
import * as PropTypes from "prop-types";
import ReactSelect from "react-select";
import * as classnames from "classnames";
import { FormElementSize, formElementSizeClassName } from "../utils/Forms";
import "react-select/dist/react-select.css";
import "./Select.less";
import "../less/forms.less";
var selectValuePropType = PropTypes.shape({
    label: PropTypes.string.isRequired,
    value: PropTypes.string.isRequired,
});
var propTypes = {
    id: PropTypes.string.isRequired,
    name: PropTypes.string.isRequired,
    clearable: PropTypes.bool,
    disabled: PropTypes.bool,
    label: PropTypes.string,
    multi: PropTypes.bool,
    onFocus: PropTypes.func,
    isLoading: PropTypes.bool,
    onChange: PropTypes.func,
    optionRenderer: PropTypes.func,
    options: PropTypes.arrayOf(selectValuePropType),
    lazy: PropTypes.bool,
    loadOptions: PropTypes.func,
    filterOptions: PropTypes.func,
    placeholder: PropTypes.string,
    readOnly: PropTypes.bool,
    searchable: PropTypes.bool,
    creatable: PropTypes.bool,
    creatablePromptFn: PropTypes.func,
    noResultsText: PropTypes.node,
    required: PropTypes.bool,
    value: PropTypes.oneOfType([
        PropTypes.string,
        selectValuePropType,
        PropTypes.arrayOf(PropTypes.string),
        PropTypes.arrayOf(selectValuePropType),
    ]),
    className: PropTypes.string,
    error: PropTypes.string,
    size: PropTypes.oneOf(Object.values(FormElementSize)),
    closeMenuOnSelect: PropTypes.bool,
    ariaLabel: PropTypes.string,
};
var defaultProps = {
    clearable: false,
    placeholder: "",
    searchable: false,
    creatable: false,
    closeMenuOnSelect: true,
    size: FormElementSize.FULL_WIDTH,
};
export var cssClass = {
    CONTAINER: "Select--container",
    LABEL: "Select--label",
    LABEL_CONTAINER: "Select--labelContainer",
    LABEL_HIDDEN: "Select--labelHidden",
    REACT_SELECT: "Select--ReactSelect",
    READ_ONLY: "Select--readOnly",
};
function isLabelHidden(placeholder, value) {
    if (!placeholder) {
        return false;
    }
    if (!value) {
        return true;
    }
    if (Array.isArray(value) && value.length === 0) {
        return true;
    }
    return false;
}
/*
 * Component to allow selecting options from a list. It can fetch those options
 * synchronously or asynchronously by specifying the `lazy` parameter.
 */
var Select = /** @class */ (function (_super) {
    __extends(Select, _super);
    function Select(props) {
        var _this = _super.call(this, props) || this;
        _this.state = { hasBeenFocused: false };
        return _this;
    }
    Select.prototype.render = function () {
        var _this = this;
        var _a = this.props, id = _a.id, name = _a.name, clearable = _a.clearable, disabled = _a.disabled, label = _a.label, multi = _a.multi, onFocus = _a.onFocus, isLoading = _a.isLoading, onChange = _a.onChange, optionRenderer = _a.optionRenderer, options = _a.options, lazy = _a.lazy, loadOptions = _a.loadOptions, filterOptions = _a.filterOptions, _b = _a.placeholder, placeholder = _b === void 0 ? "" : _b, readOnly = _a.readOnly, required = _a.required, searchable = _a.searchable, noResultsText = _a.noResultsText, creatable = _a.creatable, creatablePromptFn = _a.creatablePromptFn, value = _a.value, className = _a.className, error = _a.error, size = _a.size, closeMenuOnSelect = _a.closeMenuOnSelect, ariaLabel = _a.ariaLabel;
        if (!lazy) {
            if (!options) {
                console.warn('Select: prop "options" must be set if not "lazy"');
            }
            if (loadOptions) {
                console.warn('Select: prop "loadOptions" may not be set if not "lazy"');
            }
        }
        else {
            if (options) {
                console.warn('Select: prop "options" may not be set if "lazy"');
            }
            if (!loadOptions) {
                console.warn('Select: prop "loadOptions" must be set if "lazy"');
            }
            if (isLoading) {
                console.warn('Select: prop "isLoading" may not be set if "lazy"');
            }
        }
        var labelClasses = cssClass.LABEL;
        if (isLabelHidden(placeholder, value)) {
            labelClasses += " " + cssClass.LABEL_HIDDEN;
        }
        var inputNote;
        if (required) {
            inputNote = React.createElement("span", { className: "Select--required" }, "required");
        }
        var wrapperClass = className;
        if (required && this.state.hasBeenFocused && !value) {
            // error should overwrite required
            inputNote = React.createElement("span", { className: "Select--error" }, "required");
            wrapperClass += " Select--hasError";
        }
        if (error) {
            // error should overwrite required
            inputNote = React.createElement("span", { className: "Select--error" }, error);
            wrapperClass += " Select--hasError";
        }
        var reactSelectClasses = cssClass.REACT_SELECT;
        if (readOnly) {
            reactSelectClasses += " " + cssClass.READ_ONLY;
        }
        var SelectComponent = ReactSelect;
        if (creatable && lazy) {
            SelectComponent = ReactSelect.AsyncCreatable;
        }
        else if (creatable) {
            SelectComponent = ReactSelect.Creatable;
        }
        else if (lazy) {
            SelectComponent = ReactSelect.Async;
        }
        var overrideProps = lazy && !filterOptions ? { filterOptions: function (results) { return results; } } : {};
        // The label container must be returned after the ReactSelect otherwise it does not get displayed
        // in the browser.
        return (React.createElement("div", { className: classnames(cssClass.CONTAINER, formElementSizeClassName(size), wrapperClass) },
            React.createElement("div", { id: id },
                React.createElement(SelectComponent, __assign({ className: reactSelectClasses, clearable: clearable, promptTextCreator: creatablePromptFn, disabled: disabled || readOnly, multi: multi, name: name, onBlur: function () { return _this.setState({ hasBeenFocused: true }); }, onChange: onChange, onFocus: onFocus, optionRenderer: optionRenderer, options: options, loadOptions: loadOptions, isLoading: isLoading, filterOptions: filterOptions, placeholder: placeholder, searchable: searchable, noResultsText: noResultsText, closeOnSelect: closeMenuOnSelect, value: value, role: "listbox", "aria-label": ariaLabel }, overrideProps))),
            React.createElement("div", { className: cssClass.LABEL_CONTAINER },
                React.createElement("label", { className: labelClasses, htmlFor: id }, label),
                inputNote)));
    };
    Select.propTypes = propTypes;
    Select.defaultProps = defaultProps;
    return Select;
}(React.Component));
export { Select };
