function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { forwardRef, useImperativeHandle, useMemo, useRef, useState } from "react";
import { useOutsideClick, useForkRef } from "../../hooks";
import { cx } from "../../utils";
import { base as sizeCSS } from "./variations/_size/base";
import { base as viewCSS } from "./variations/_view/base";
import { base as hintViewCSS } from "./variations/_hint-view/base";
import { base as hintSizeCSS } from "./variations/_hint-size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as readOnlyCSS } from "./variations/_readonly/base";
import { getChangeHandler, getSearchHandler } from "./utils";
import { ContentWrapper, StyledContentLeft, StyledContentRight, StyledContentRightWrapper, StyledDivider, StyledInput, base } from "./Range.styles";
import { classes } from "./Range.tokens";
import { HintComponent, InformationWrapperUI } from "./ui";
export var rangeRoot = function rangeRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), className = _ref2.className, // controlled
        firstValue = _ref2.firstValue, secondValue = _ref2.secondValue, firstValueError = _ref2.firstValueError, secondValueError = _ref2.secondValueError, firstValueSuccess = _ref2.firstValueSuccess, secondValueSuccess = _ref2.secondValueSuccess, firstPlaceholder = _ref2.firstPlaceholder, secondPlaceholder = _ref2.secondPlaceholder, // variations
        appearance = _ref2.appearance, hasClearDivider = _ref2.hasClearDivider, view = _ref2.view, size = _ref2.size, _ref_readOnly = _ref2.readOnly, readOnly = _ref_readOnly === void 0 ? false : _ref_readOnly, _ref_disabled = _ref2.disabled, disabled = _ref_disabled === void 0 ? false : _ref_disabled, _ref_required = _ref2.required, required = _ref_required === void 0 ? false : _ref_required, _ref_requiredIndicatorPlacement = _ref2.requiredIndicatorPlacement, requiredIndicatorPlacement = _ref_requiredIndicatorPlacement === void 0 ? 'right' : _ref_requiredIndicatorPlacement, _ref_hasRequiredIndicator = _ref2.hasRequiredIndicator, hasRequiredIndicator = _ref_hasRequiredIndicator === void 0 ? false : _ref_hasRequiredIndicator, _ref_hintTrigger = _ref2.// hint
        hintTrigger, hintTrigger = _ref_hintTrigger === void 0 ? 'hover' : _ref_hintTrigger, hintText = _ref2.hintText, _ref_hintView = _ref2.hintView, hintView = _ref_hintView === void 0 ? 'default' : _ref_hintView, _ref_hintSize = _ref2.hintSize, hintSize = _ref_hintSize === void 0 ? 'm' : _ref_hintSize, hintTargetIcon = _ref2.hintTargetIcon, _ref_hintTargetPlacement = _ref2.hintTargetPlacement, hintTargetPlacement = _ref_hintTargetPlacement === void 0 ? 'outer' : _ref_hintTargetPlacement, _ref_hintPlacement = _ref2.hintPlacement, hintPlacement = _ref_hintPlacement === void 0 ? 'auto' : _ref_hintPlacement, hintHasArrow = _ref2.hintHasArrow, _ref_hintOffset = _ref2.hintOffset, hintOffset = _ref_hintOffset === void 0 ? [
            0,
            0
        ] : _ref_hintOffset, hintWidth = _ref2.hintWidth, hintContentLeft = _ref2.hintContentLeft, // layout
        label = _ref2.label, leftHelper = _ref2.leftHelper, titleCaption = _ref2.titleCaption, contentLeft = _ref2.contentLeft, contentRight = _ref2.contentRight, firstTextfieldContentLeft = _ref2.firstTextfieldContentLeft, firstTextfieldContentRight = _ref2.firstTextfieldContentRight, secondTextfieldContentLeft = _ref2.secondTextfieldContentLeft, secondTextfieldContentRight = _ref2.secondTextfieldContentRight, firstTextfieldTextBefore = _ref2.firstTextfieldTextBefore, secondTextfieldTextBefore = _ref2.secondTextfieldTextBefore, firstTextfieldTextAfter = _ref2.firstTextfieldTextAfter, secondTextfieldTextAfter = _ref2.secondTextfieldTextAfter, _ref_dividerVariant = _ref2.dividerVariant, dividerVariant = _ref_dividerVariant === void 0 ? 'dash' : _ref_dividerVariant, dividerIcon = _ref2.dividerIcon, autoComplete = _ref2.autoComplete, onChangeFirstValue = _ref2.onChangeFirstValue, onChangeSecondValue = _ref2.onChangeSecondValue, onSearchFirstValue = _ref2.onSearchFirstValue, onSearchSecondValue = _ref2.onSearchSecondValue, onFocusFirstTextfield = _ref2.onFocusFirstTextfield, onFocusSecondTextfield = _ref2.onFocusSecondTextfield, onBlurFirstTextfield = _ref2.onBlurFirstTextfield, onBlurSecondTextfield = _ref2.onBlurSecondTextfield, rest = _object_without_properties(_ref2, [
            "className",
            "firstValue",
            "secondValue",
            "firstValueError",
            "secondValueError",
            "firstValueSuccess",
            "secondValueSuccess",
            "firstPlaceholder",
            "secondPlaceholder",
            "appearance",
            "hasClearDivider",
            "view",
            "size",
            "readOnly",
            "disabled",
            "required",
            "requiredIndicatorPlacement",
            "hasRequiredIndicator",
            "hintTrigger",
            "hintText",
            "hintView",
            "hintSize",
            "hintTargetIcon",
            "hintTargetPlacement",
            "hintPlacement",
            "hintHasArrow",
            "hintOffset",
            "hintWidth",
            "hintContentLeft",
            "label",
            "leftHelper",
            "titleCaption",
            "contentLeft",
            "contentRight",
            "firstTextfieldContentLeft",
            "firstTextfieldContentRight",
            "secondTextfieldContentLeft",
            "secondTextfieldContentRight",
            "firstTextfieldTextBefore",
            "secondTextfieldTextBefore",
            "firstTextfieldTextAfter",
            "secondTextfieldTextAfter",
            "dividerVariant",
            "dividerIcon",
            "autoComplete",
            "onChangeFirstValue",
            "onChangeSecondValue",
            "onSearchFirstValue",
            "onSearchSecondValue",
            "onFocusFirstTextfield",
            "onFocusSecondTextfield",
            "onBlurFirstTextfield",
            "onBlurSecondTextfield"
        ]), _rest1 = _sliced_to_array(_rest, 1), ref = _rest1[0];
        var rangeRef = useRef(null);
        var firstTextFieldRef = useRef(null);
        var secondTextFieldRef = useRef(null);
        var hintRef = useRef(null);
        var _useState = _sliced_to_array(useState(false), 2), isHintVisible = _useState[0], setIsHintVisible = _useState[1];
        var noCaptionAndLabelClass = !label && !titleCaption ? classes.noCaptionAndLabel : undefined;
        var clearClass = appearance === 'clear' ? classes.clear : undefined;
        var clearDividerClass = hasClearDivider ? classes.clearDivider : undefined;
        var clearOuterHintClass = clearClass && hintText && hintTargetPlacement === 'outer' ? classes.clearHasOuterHint : undefined;
        var rangeErrorClass = firstValueError && secondValueError ? classes.rangeError : undefined;
        var firstValueErrorClass = !rangeErrorClass && firstValueError ? classes.rangeValueError : undefined;
        var secondValueErrorClass = !rangeErrorClass && secondValueError ? classes.rangeValueError : undefined;
        var rangeSuccessClass = firstValueSuccess && secondValueSuccess ? classes.rangeSuccess : undefined;
        var firstValueSuccessClass = !rangeSuccessClass && firstValueSuccess ? classes.rangeValueSuccess : undefined;
        var secondValueSuccessClass = !rangeSuccessClass && secondValueSuccess ? classes.rangeValueSuccess : undefined;
        var hintInnerRef = useRef(null);
        var hintForkRef = useForkRef(hintRef, hintInnerRef);
        var handleChangeFirstValue = getChangeHandler(onChangeFirstValue, disabled, readOnly);
        var handleChangeSecondValue = getChangeHandler(onChangeSecondValue, disabled, readOnly);
        var handleSearchFirstValue = getSearchHandler(onSearchFirstValue, disabled, readOnly);
        var handleSearchSecondValue = getSearchHandler(onSearchSecondValue, disabled, readOnly);
        var handleHintShow = function handleHintShow() {
            if (disabled) {
                return;
            }
            setIsHintVisible(true);
        };
        var handleHintHide = function handleHintHide() {
            return setIsHintVisible(false);
        };
        var handleHintClick = function handleHintClick(event) {
            var _hintInnerRef_current;
            if (!hintText || hintTrigger !== 'click' || disabled) {
                return;
            }
            event.stopPropagation();
            var targetIsPopover = event.target === hintInnerRef.current;
            var rootHasTarget = (_hintInnerRef_current = hintInnerRef.current) === null || _hintInnerRef_current === void 0 ? void 0 : _hintInnerRef_current.contains(event.target);
            if (!targetIsPopover && !rootHasTarget) {
                setIsHintVisible(true);
            }
        };
        useOutsideClick(function() {
            setIsHintVisible(false);
        }, [
            hintRef
        ]);
        useImperativeHandle(ref, function() {
            return {
                firstTextField: function firstTextField() {
                    return firstTextFieldRef;
                },
                secondTextField: function secondTextField() {
                    return secondTextFieldRef;
                }
            };
        }, [
            firstTextFieldRef,
            secondTextFieldRef
        ]);
        var Divider = useMemo(function() {
            if (dividerVariant === 'none') {
                return null;
            }
            if (dividerVariant === 'dash') {
                return /*#__PURE__*/ React.createElement(StyledDivider, null, "–");
            }
            return /*#__PURE__*/ React.createElement(StyledDivider, null, dividerIcon);
        }, [
            dividerVariant,
            dividerIcon
        ]);
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            ref: rangeRef,
            className: cx(clearClass, noCaptionAndLabelClass, className),
            view: view,
            size: size,
            disabled: disabled,
            readOnly: !disabled && readOnly
        }, hintText && {
            hintView: hintView,
            hintSize: hintSize
        }, rest), /*#__PURE__*/ React.createElement(InformationWrapperUI, _object_spread({
            className: cx(rangeErrorClass, firstValueErrorClass, secondValueErrorClass, rangeSuccessClass, firstValueSuccessClass, secondValueSuccessClass, clearOuterHintClass),
            size: size,
            label: label,
            leftHelper: leftHelper,
            titleCaption: titleCaption,
            required: required,
            requiredIndicatorPlacement: requiredIndicatorPlacement,
            hasRequiredIndicator: hasRequiredIndicator,
            disabled: disabled
        }, hintText && hintTargetPlacement === 'outer' ? {
            hintText: hintText,
            hintView: hintView,
            hintSize: hintSize,
            hintTrigger: hintTrigger,
            hintTargetIcon: hintTargetIcon,
            hintPlacement: hintPlacement,
            hintHasArrow: hintHasArrow,
            hintOffset: hintOffset,
            hintWidth: hintWidth,
            hintContentLeft: hintContentLeft
        } : {
            hintText: undefined
        }), /*#__PURE__*/ React.createElement(ContentWrapper, {
            className: cx(clearClass, clearDividerClass, rangeErrorClass, rangeSuccessClass)
        }, contentLeft && /*#__PURE__*/ React.createElement(StyledContentLeft, null, contentLeft), /*#__PURE__*/ React.createElement(StyledInput, {
            ref: firstTextFieldRef,
            className: cx(firstValueErrorClass, firstValueSuccessClass),
            value: firstValue,
            readOnly: readOnly,
            disabled: disabled,
            placeholder: firstPlaceholder,
            contentLeft: firstTextfieldContentLeft,
            contentRight: firstTextfieldContentRight,
            textBefore: firstTextfieldTextBefore,
            textAfter: firstTextfieldTextAfter,
            onChange: handleChangeFirstValue,
            onSearch: handleSearchFirstValue,
            onFocus: onFocusFirstTextfield,
            onBlur: onBlurFirstTextfield,
            autoComplete: autoComplete,
            "data-error": firstValueError,
            "data-success": firstValueSuccess,
            hasDivider: hasClearDivider
        }), Divider, /*#__PURE__*/ React.createElement(StyledInput, {
            ref: secondTextFieldRef,
            className: cx(secondValueErrorClass, secondValueSuccessClass),
            value: secondValue,
            readOnly: readOnly,
            disabled: disabled,
            placeholder: secondPlaceholder,
            contentLeft: secondTextfieldContentLeft,
            contentRight: secondTextfieldContentRight,
            textBefore: secondTextfieldTextBefore,
            textAfter: secondTextfieldTextAfter,
            onChange: handleChangeSecondValue,
            onSearch: handleSearchSecondValue,
            onFocus: onFocusSecondTextfield,
            onBlur: onBlurSecondTextfield,
            autoComplete: autoComplete,
            hasDivider: hasClearDivider
        }), /*#__PURE__*/ React.createElement(StyledContentRightWrapper, null, !label && hintText && hintTargetPlacement === 'inner' && /*#__PURE__*/ React.createElement(HintComponent, {
            ref: hintForkRef,
            hintText: hintText,
            hintTrigger: hintTrigger,
            isHintVisible: isHintVisible,
            hintTargetIcon: hintTargetIcon,
            hintPlacement: hintPlacement,
            hintHasArrow: hintHasArrow,
            hintOffset: hintOffset,
            hintWidth: hintWidth,
            hintContentLeft: hintContentLeft,
            size: size,
            handleHintShow: handleHintShow,
            handleHintHide: handleHintHide,
            handleHintClick: handleHintClick,
            withoutLabel: true
        }), contentRight && /*#__PURE__*/ React.createElement(StyledContentRight, null, contentRight)))));
    });
};
export var rangeConfig = {
    name: 'Range',
    tag: 'div',
    layout: rangeRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        hintView: {
            css: hintViewCSS
        },
        hintSize: {
            css: hintSizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        readOnly: {
            css: readOnlyCSS,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        hintView: 'default',
        hintSize: 'm'
    }
};
