"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get rangeConfig () {
        return rangeConfig;
    },
    get rangeRoot () {
        return rangeRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("../../hooks");
const _utils = require("../../utils");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _base2 = require("./variations/_hint-view/base");
const _base3 = require("./variations/_hint-size/base");
const _base4 = require("./variations/_disabled/base");
const _base5 = require("./variations/_readonly/base");
const _utils1 = require("./utils");
const _Rangestyles = require("./Range.styles");
const _Rangetokens = require("./Range.tokens");
const _ui = require("./ui");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const rangeRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ className, // controlled
    firstValue, secondValue, firstValueError, secondValueError, firstValueSuccess, secondValueSuccess, firstValueEdited, secondValueEdited, firstPlaceholder, secondPlaceholder, // variations
    appearance, hasClearDivider, view, size, readOnly = false, disabled = false, required = false, requiredIndicatorPlacement = 'right', hasRequiredIndicator = false, // hint
    hintTrigger = 'hover', hintText, hintView = 'default', hintSize = 'm', hintTargetIcon, hintTargetPlacement = 'outer', hintPlacement = 'auto', hintHasArrow, hintOffset = [
        0,
        0
    ], hintWidth, hintContentLeft, // layout
    label, leftHelper, titleCaption, contentLeft, contentRight, firstTextfieldContentLeft, firstTextfieldContentRight, secondTextfieldContentLeft, secondTextfieldContentRight, firstTextfieldTextBefore, secondTextfieldTextBefore, firstTextfieldTextAfter, secondTextfieldTextAfter, dividerVariant = 'dash', dividerIcon, autoComplete, onChangeFirstValue, onChangeSecondValue, onSearchFirstValue, onSearchSecondValue, onFocusFirstTextfield, onFocusSecondTextfield, onBlurFirstTextfield, onBlurSecondTextfield, ...rest }, ref)=>{
        const rangeRef = (0, _react.useRef)(null);
        const firstTextFieldRef = (0, _react.useRef)(null);
        const secondTextFieldRef = (0, _react.useRef)(null);
        const hintRef = (0, _react.useRef)(null);
        const [isHintVisible, setIsHintVisible] = (0, _react.useState)(false);
        const noCaptionAndLabelClass = !label && !titleCaption ? _Rangetokens.classes.noCaptionAndLabel : undefined;
        const clearClass = appearance === 'clear' ? _Rangetokens.classes.clear : undefined;
        const clearDividerClass = hasClearDivider ? _Rangetokens.classes.clearDivider : undefined;
        const clearOuterHintClass = clearClass && hintText && hintTargetPlacement === 'outer' ? _Rangetokens.classes.clearHasOuterHint : undefined;
        const rangeErrorClass = firstValueError && secondValueError ? _Rangetokens.classes.rangeError : undefined;
        const firstValueErrorClass = !rangeErrorClass && firstValueError ? _Rangetokens.classes.rangeValueError : undefined;
        const secondValueErrorClass = !rangeErrorClass && secondValueError ? _Rangetokens.classes.rangeValueError : undefined;
        const rangeSuccessClass = firstValueSuccess && secondValueSuccess ? _Rangetokens.classes.rangeSuccess : undefined;
        const firstValueSuccessClass = !rangeSuccessClass && firstValueSuccess ? _Rangetokens.classes.rangeValueSuccess : undefined;
        const secondValueSuccessClass = !rangeSuccessClass && secondValueSuccess ? _Rangetokens.classes.rangeValueSuccess : undefined;
        const rangeEditedClass = firstValueEdited && secondValueEdited ? _Rangetokens.classes.rangeEdited : undefined;
        const firstValueEditedClass = !rangeEditedClass && firstValueEdited ? _Rangetokens.classes.rangeValueEdited : undefined;
        const secondValueEditedClass = !rangeEditedClass && secondValueEdited ? _Rangetokens.classes.rangeValueEdited : undefined;
        const hintInnerRef = (0, _react.useRef)(null);
        const hintForkRef = (0, _hooks.useForkRef)(hintRef, hintInnerRef);
        const handleChangeFirstValue = (0, _utils1.getChangeHandler)(onChangeFirstValue, disabled, readOnly);
        const handleChangeSecondValue = (0, _utils1.getChangeHandler)(onChangeSecondValue, disabled, readOnly);
        const handleSearchFirstValue = (0, _utils1.getSearchHandler)(onSearchFirstValue, disabled, readOnly);
        const handleSearchSecondValue = (0, _utils1.getSearchHandler)(onSearchSecondValue, disabled, readOnly);
        const handleHintShow = ()=>{
            if (disabled) {
                return;
            }
            setIsHintVisible(true);
        };
        const handleHintHide = ()=>setIsHintVisible(false);
        const handleHintClick = (event)=>{
            if (!hintText || hintTrigger !== 'click' || disabled) {
                return;
            }
            event.stopPropagation();
            const targetIsPopover = event.target === hintInnerRef.current;
            const rootHasTarget = hintInnerRef.current?.contains(event.target);
            if (!targetIsPopover && !rootHasTarget) {
                setIsHintVisible(true);
            }
        };
        (0, _hooks.useOutsideClick)(()=>{
            setIsHintVisible(false);
        }, [
            hintRef
        ]);
        (0, _react.useImperativeHandle)(ref, ()=>({
                firstTextField: ()=>{
                    return firstTextFieldRef;
                },
                secondTextField: ()=>{
                    return secondTextFieldRef;
                }
            }), [
            firstTextFieldRef,
            secondTextFieldRef
        ]);
        const Divider = (0, _react.useMemo)(()=>{
            if (dividerVariant === 'none') {
                return null;
            }
            if (dividerVariant === 'dash') {
                return /*#__PURE__*/ _react.default.createElement(_Rangestyles.StyledDivider, null, "–");
            }
            return /*#__PURE__*/ _react.default.createElement(_Rangestyles.StyledDivider, null, dividerIcon);
        }, [
            dividerVariant,
            dividerIcon
        ]);
        return /*#__PURE__*/ _react.default.createElement(Root, {
            ref: rangeRef,
            className: (0, _utils.cx)(clearClass, noCaptionAndLabelClass, className),
            view: view,
            size: size,
            disabled: disabled,
            readOnly: !disabled && readOnly,
            ...hintText && {
                hintView,
                hintSize
            },
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_ui.InformationWrapperUI, {
            className: (0, _utils.cx)(rangeErrorClass, firstValueErrorClass, secondValueErrorClass, rangeSuccessClass, firstValueSuccessClass, secondValueSuccessClass, rangeEditedClass, firstValueEditedClass, secondValueEditedClass, clearOuterHintClass),
            size: size,
            label: label,
            leftHelper: leftHelper,
            titleCaption: titleCaption,
            required: required,
            requiredIndicatorPlacement: requiredIndicatorPlacement,
            hasRequiredIndicator: hasRequiredIndicator,
            disabled: disabled,
            ...hintText && hintTargetPlacement === 'outer' ? {
                hintText,
                hintView,
                hintSize,
                hintTrigger,
                hintTargetIcon,
                hintPlacement,
                hintHasArrow,
                hintOffset,
                hintWidth,
                hintContentLeft
            } : {
                hintText: undefined
            }
        }, /*#__PURE__*/ _react.default.createElement(_Rangestyles.ContentWrapper, {
            className: (0, _utils.cx)(clearClass, clearDividerClass, rangeErrorClass, rangeSuccessClass, rangeEditedClass)
        }, contentLeft && /*#__PURE__*/ _react.default.createElement(_Rangestyles.StyledContentLeft, null, contentLeft), /*#__PURE__*/ _react.default.createElement(_Rangestyles.StyledInput, {
            ref: firstTextFieldRef,
            className: (0, _utils.cx)(firstValueErrorClass, firstValueSuccessClass, firstValueEditedClass),
            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.default.createElement(_Rangestyles.StyledInput, {
            ref: secondTextFieldRef,
            className: (0, _utils.cx)(secondValueErrorClass, secondValueSuccessClass, secondValueEditedClass),
            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.default.createElement(_Rangestyles.StyledContentRightWrapper, null, !label && hintText && hintTargetPlacement === 'inner' && /*#__PURE__*/ _react.default.createElement(_ui.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.default.createElement(_Rangestyles.StyledContentRight, null, contentRight)))));
    });
const rangeConfig = {
    name: 'Range',
    tag: 'div',
    layout: rangeRoot,
    base: _Rangestyles.base,
    variations: {
        view: {
            css: _base1.base
        },
        size: {
            css: _base.base
        },
        hintView: {
            css: _base2.base
        },
        hintSize: {
            css: _base3.base
        },
        disabled: {
            css: _base4.base,
            attrs: true
        },
        readOnly: {
            css: _base5.base,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        hintView: 'default',
        hintSize: 'm'
    }
};
