'use strict';

var React = require('react');
var omit = require('../omit-cjs.js');
var FormField = require('../FormField-cjs.js');
var classnames = require('classnames');
require('../tslib.es6-cjs.js');
require('react-hook-form');
require('@jobber/hooks');
require('framer-motion');
require('@jobber/design');
require('../Button-cjs.js');
var filterDataAttributes = require('../filterDataAttributes-cjs.js');
require('../_commonjsHelpers-cjs.js');
require('../_getAllKeys-cjs.js');
require('../isTypedArray-cjs.js');
require('../isObjectLike-cjs.js');
require('../isSymbol-cjs.js');
require('../identity-cjs.js');
require('../_getTag-cjs.js');
require('../keysIn-cjs.js');
require('../_baseAssignValue-cjs.js');
require('../_baseFlatten-cjs.js');
require('../_setToString-cjs.js');
require('../Icon-cjs.js');
require('../Text-cjs.js');
require('../Typography-cjs.js');
require('../useFormFieldFocus-cjs.js');
require('../InputValidation-cjs.js');
require('../Spinner-cjs.js');
require('react-router-dom');

// eslint-disable-next-line import/no-deprecated
function Select$1(props) {
    const formFieldProps = omit.omit(Object.assign({}, props), ["version"]);
    return React.createElement(FormField.FormField, Object.assign({ type: "select" }, formFieldProps));
}

/**
 * Hook with actions on the Select component.
 */
function useSelectActions({ onChange, onBlur, onFocus, }) {
    function handleChange(event) {
        const newValue = event.currentTarget.value;
        onChange === null || onChange === void 0 ? void 0 : onChange(newValue, event);
    }
    function handleBlur(event) {
        onBlur === null || onBlur === void 0 ? void 0 : onBlur(event);
    }
    function handleFocus(event) {
        onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
    }
    return {
        handleChange,
        handleBlur,
        handleFocus,
    };
}

var styles = {"select":"-hmXAsAfH9U-","selectPostfix":"_5ST4c1fXDYU-","spinning":"oOACJmrVDf0-"};

function SelectRebuilt(props) {
    var _a;
    const { mergedRef, wrapperRef } = useSelectRefs(props.inputRef);
    const dataAttrs = filterDataAttributes.filterDataAttributes(props);
    const generatedId = React.useId();
    const id = props.id || generatedId;
    const descriptionIdentifier = `descriptionUUID--${id}`;
    const { name } = FormField.useAtlantisFormFieldName({
        nameProp: props.name,
        id: id,
    });
    const { handleChange, handleBlur, handleFocus } = useSelectActions({
        onChange: props.onChange,
        onBlur: props.onBlur,
        onFocus: props.onFocus,
    });
    const descriptionVisible = props.description && !props.inline;
    const isInvalid = Boolean(props.error || props.invalid);
    return (React.createElement(FormField.FormFieldWrapper, { disabled: props.disabled, size: props.size, align: props.align, inline: props.inline, name: name, wrapperRef: wrapperRef, error: (_a = props.error) !== null && _a !== void 0 ? _a : "", invalid: props.invalid, identifier: id, descriptionIdentifier: descriptionIdentifier, description: props.description, type: "select", placeholder: props.placeholder, value: props.value, prefix: props.prefix, suffix: props.suffix, clearable: "never" },
        React.createElement(React.Fragment, null,
            React.createElement("select", Object.assign({ id: id, name: name, disabled: props.disabled, autoFocus: props.autoFocus, required: props.required, onChange: handleChange, onBlur: handleBlur, onFocus: handleFocus, value: props.value, "aria-label": props["aria-label"], "aria-describedby": descriptionVisible
                    ? descriptionIdentifier
                    : props["aria-describedby"], "aria-invalid": isInvalid ? true : undefined, "aria-required": props["aria-required"], ref: mergedRef, className: classnames(FormField.formFieldStyles.input, props.UNSAFE_experimentalStyles && styles.select) }, dataAttrs), props.children),
            React.createElement(FormField.FormFieldPostFix, { variation: "select", className: styles.selectPostfix }))));
}
function useSelectRefs(inputRef) {
    const internalRef = React.useRef(null);
    const mergedRef = FormField.mergeRefs([internalRef, inputRef]);
    const wrapperRef = React.useRef(null);
    return { mergedRef, wrapperRef };
}

function SelectOption({ children, disabled, value }) {
    return (React.createElement("option", { disabled: disabled, value: value }, children));
}

function OptionGroup({ children, label, disabled, UNSAFE_className, UNSAFE_style, }) {
    return (React.createElement("optgroup", { label: label, disabled: disabled, className: UNSAFE_className, style: UNSAFE_style === null || UNSAFE_style === void 0 ? void 0 : UNSAFE_style.container }, children));
}

function isNewSelectProps(props) {
    return props.version === 2;
}
function Select(props) {
    if (isNewSelectProps(props)) {
        return React.createElement(SelectRebuilt, Object.assign({}, props));
    }
    else {
        return React.createElement(Select$1, Object.assign({}, props));
    }
}
Select.Option = SelectOption;
Select.OptionGroup = OptionGroup;

exports.Option = SelectOption;
exports.Select = Select;
