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 const rangeRoot = (Root)=>/*#__PURE__*/ 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 = useRef(null);
        const firstTextFieldRef = useRef(null);
        const secondTextFieldRef = useRef(null);
        const hintRef = useRef(null);
        const [isHintVisible, setIsHintVisible] = useState(false);
        const noCaptionAndLabelClass = !label && !titleCaption ? classes.noCaptionAndLabel : undefined;
        const clearClass = appearance === 'clear' ? classes.clear : undefined;
        const clearDividerClass = hasClearDivider ? classes.clearDivider : undefined;
        const clearOuterHintClass = clearClass && hintText && hintTargetPlacement === 'outer' ? classes.clearHasOuterHint : undefined;
        const rangeErrorClass = firstValueError && secondValueError ? classes.rangeError : undefined;
        const firstValueErrorClass = !rangeErrorClass && firstValueError ? classes.rangeValueError : undefined;
        const secondValueErrorClass = !rangeErrorClass && secondValueError ? classes.rangeValueError : undefined;
        const rangeSuccessClass = firstValueSuccess && secondValueSuccess ? classes.rangeSuccess : undefined;
        const firstValueSuccessClass = !rangeSuccessClass && firstValueSuccess ? classes.rangeValueSuccess : undefined;
        const secondValueSuccessClass = !rangeSuccessClass && secondValueSuccess ? classes.rangeValueSuccess : undefined;
        const rangeEditedClass = firstValueEdited && secondValueEdited ? classes.rangeEdited : undefined;
        const firstValueEditedClass = !rangeEditedClass && firstValueEdited ? classes.rangeValueEdited : undefined;
        const secondValueEditedClass = !rangeEditedClass && secondValueEdited ? classes.rangeValueEdited : undefined;
        const hintInnerRef = useRef(null);
        const hintForkRef = useForkRef(hintRef, hintInnerRef);
        const handleChangeFirstValue = getChangeHandler(onChangeFirstValue, disabled, readOnly);
        const handleChangeSecondValue = getChangeHandler(onChangeSecondValue, disabled, readOnly);
        const handleSearchFirstValue = getSearchHandler(onSearchFirstValue, disabled, readOnly);
        const handleSearchSecondValue = 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);
            }
        };
        useOutsideClick(()=>{
            setIsHintVisible(false);
        }, [
            hintRef
        ]);
        useImperativeHandle(ref, ()=>({
                firstTextField: ()=>{
                    return firstTextFieldRef;
                },
                secondTextField: ()=>{
                    return secondTextFieldRef;
                }
            }), [
            firstTextFieldRef,
            secondTextFieldRef
        ]);
        const Divider = useMemo(()=>{
            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, {
            ref: rangeRef,
            className: cx(clearClass, noCaptionAndLabelClass, className),
            view: view,
            size: size,
            disabled: disabled,
            readOnly: !disabled && readOnly,
            ...hintText && {
                hintView,
                hintSize
            },
            ...rest
        }, /*#__PURE__*/ React.createElement(InformationWrapperUI, {
            className: 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.createElement(ContentWrapper, {
            className: cx(clearClass, clearDividerClass, rangeErrorClass, rangeSuccessClass, rangeEditedClass)
        }, contentLeft && /*#__PURE__*/ React.createElement(StyledContentLeft, null, contentLeft), /*#__PURE__*/ React.createElement(StyledInput, {
            ref: firstTextFieldRef,
            className: 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.createElement(StyledInput, {
            ref: secondTextFieldRef,
            className: 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.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 const rangeConfig = {
    name: 'Range',
    tag: 'div',
    layout: rangeRoot,
    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'
    }
};
