import { objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useRef, useState, useImperativeHandle, useMemo } from 'react';
import { cx } from '../../utils/index.js';
import { base as base$2 } from './variations/_size/base.js';
import { base as base$1 } from './variations/_view/base.js';
import { base as base$3 } from './variations/_hint-view/base.js';
import { base as base$4 } from './variations/_hint-size/base.js';
import { base as base$5 } from './variations/_disabled/base.js';
import { base as base$6 } from './variations/_readonly/base.js';
import { getChangeHandler, getSearchHandler } from './utils/index.js';
import { StyledDivider, ContentWrapper, StyledContentLeft, StyledInput, StyledContentRightWrapper, StyledContentRight, base } from './Range.styles.js';
import { classes } from './Range.tokens.js';
import { useForkRef } from '../../hooks/useForkRef.js';
import { useOutsideClick } from '../../hooks/useOutsideClick.js';
import { InformationWrapperUI } from './ui/InformationWrapper/InformationWrapper.styles.js';
import { HintComponent } from '../InformationWrapper/ui/Hint/Hint.js';

var _StyledDivider;
var _excluded = ["className", "firstValue", "secondValue", "firstValueError", "secondValueError", "firstValueSuccess", "secondValueSuccess", "firstValueEdited", "secondValueEdited", "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"];
var rangeRoot = function rangeRoot(Root) {
  return /*#__PURE__*/forwardRef(function (_ref, ref) {
    var _StyledDivider2;
    var className = _ref.className,
      firstValue = _ref.firstValue,
      secondValue = _ref.secondValue,
      firstValueError = _ref.firstValueError,
      secondValueError = _ref.secondValueError,
      firstValueSuccess = _ref.firstValueSuccess,
      secondValueSuccess = _ref.secondValueSuccess,
      firstValueEdited = _ref.firstValueEdited,
      secondValueEdited = _ref.secondValueEdited,
      firstPlaceholder = _ref.firstPlaceholder,
      secondPlaceholder = _ref.secondPlaceholder,
      appearance = _ref.appearance,
      hasClearDivider = _ref.hasClearDivider,
      view = _ref.view,
      size = _ref.size,
      _ref$readOnly = _ref.readOnly,
      readOnly = _ref$readOnly === void 0 ? false : _ref$readOnly,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      _ref$required = _ref.required,
      required = _ref$required === void 0 ? false : _ref$required,
      _ref$requiredIndicato = _ref.requiredIndicatorPlacement,
      requiredIndicatorPlacement = _ref$requiredIndicato === void 0 ? 'right' : _ref$requiredIndicato,
      _ref$hasRequiredIndic = _ref.hasRequiredIndicator,
      hasRequiredIndicator = _ref$hasRequiredIndic === void 0 ? false : _ref$hasRequiredIndic,
      _ref$hintTrigger = _ref.hintTrigger,
      hintTrigger = _ref$hintTrigger === void 0 ? 'hover' : _ref$hintTrigger,
      hintText = _ref.hintText,
      _ref$hintView = _ref.hintView,
      hintView = _ref$hintView === void 0 ? 'default' : _ref$hintView,
      _ref$hintSize = _ref.hintSize,
      hintSize = _ref$hintSize === void 0 ? 'm' : _ref$hintSize,
      hintTargetIcon = _ref.hintTargetIcon,
      _ref$hintTargetPlacem = _ref.hintTargetPlacement,
      hintTargetPlacement = _ref$hintTargetPlacem === void 0 ? 'outer' : _ref$hintTargetPlacem,
      _ref$hintPlacement = _ref.hintPlacement,
      hintPlacement = _ref$hintPlacement === void 0 ? 'auto' : _ref$hintPlacement,
      hintHasArrow = _ref.hintHasArrow,
      _ref$hintOffset = _ref.hintOffset,
      hintOffset = _ref$hintOffset === void 0 ? [0, 0] : _ref$hintOffset,
      hintWidth = _ref.hintWidth,
      hintContentLeft = _ref.hintContentLeft,
      label = _ref.label,
      leftHelper = _ref.leftHelper,
      titleCaption = _ref.titleCaption,
      contentLeft = _ref.contentLeft,
      contentRight = _ref.contentRight,
      firstTextfieldContentLeft = _ref.firstTextfieldContentLeft,
      firstTextfieldContentRight = _ref.firstTextfieldContentRight,
      secondTextfieldContentLeft = _ref.secondTextfieldContentLeft,
      secondTextfieldContentRight = _ref.secondTextfieldContentRight,
      firstTextfieldTextBefore = _ref.firstTextfieldTextBefore,
      secondTextfieldTextBefore = _ref.secondTextfieldTextBefore,
      firstTextfieldTextAfter = _ref.firstTextfieldTextAfter,
      secondTextfieldTextAfter = _ref.secondTextfieldTextAfter,
      _ref$dividerVariant = _ref.dividerVariant,
      dividerVariant = _ref$dividerVariant === void 0 ? 'dash' : _ref$dividerVariant,
      dividerIcon = _ref.dividerIcon,
      autoComplete = _ref.autoComplete,
      onChangeFirstValue = _ref.onChangeFirstValue,
      onChangeSecondValue = _ref.onChangeSecondValue,
      onSearchFirstValue = _ref.onSearchFirstValue,
      onSearchSecondValue = _ref.onSearchSecondValue,
      onFocusFirstTextfield = _ref.onFocusFirstTextfield,
      onFocusSecondTextfield = _ref.onFocusSecondTextfield,
      onBlurFirstTextfield = _ref.onBlurFirstTextfield,
      onBlurSecondTextfield = _ref.onBlurSecondTextfield,
      rest = _objectWithoutProperties(_ref, _excluded);
    var rangeRef = useRef(null);
    var firstTextFieldRef = useRef(null);
    var secondTextFieldRef = useRef(null);
    var hintRef = useRef(null);
    var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      isHintVisible = _useState2[0],
      setIsHintVisible = _useState2[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 rangeEditedClass = firstValueEdited && secondValueEdited ? classes.rangeEdited : undefined;
    var firstValueEditedClass = !rangeEditedClass && firstValueEdited ? classes.rangeValueEdited : undefined;
    var secondValueEditedClass = !rangeEditedClass && secondValueEdited ? classes.rangeValueEdited : 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 _StyledDivider || (_StyledDivider = /*#__PURE__*/React__default.createElement(StyledDivider, null, "\u2013"));
      }
      return _StyledDivider2 || (_StyledDivider2 = /*#__PURE__*/React__default.createElement(StyledDivider, null, dividerIcon));
    }, [dividerVariant, dividerIcon]);
    return /*#__PURE__*/React__default.createElement(Root, _extends({
      ref: rangeRef,
      className: cx(clearClass, noCaptionAndLabelClass, className),
      view: view,
      size: size,
      disabled: disabled,
      readOnly: !disabled && readOnly
    }, hintText && {
      hintView: hintView,
      hintSize: hintSize
    }, rest), /*#__PURE__*/React__default.createElement(InformationWrapperUI, _extends({
      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: hintText,
      hintView: hintView,
      hintSize: hintSize,
      hintTrigger: hintTrigger,
      hintTargetIcon: hintTargetIcon,
      hintPlacement: hintPlacement,
      hintHasArrow: hintHasArrow,
      hintOffset: hintOffset,
      hintWidth: hintWidth,
      hintContentLeft: hintContentLeft
    } : {
      hintText: undefined
    }), /*#__PURE__*/React__default.createElement(ContentWrapper, {
      className: cx(clearClass, clearDividerClass, rangeErrorClass, rangeSuccessClass, rangeEditedClass)
    }, contentLeft && /*#__PURE__*/React__default.createElement(StyledContentLeft, null, contentLeft), /*#__PURE__*/React__default.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__default.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__default.createElement(StyledContentRightWrapper, null, !label && hintText && hintTargetPlacement === 'inner' && /*#__PURE__*/React__default.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__default.createElement(StyledContentRight, null, contentRight)))));
  });
};
var rangeConfig = {
  name: 'Range',
  tag: 'div',
  layout: rangeRoot,
  base: base,
  variations: {
    view: {
      css: base$1
    },
    size: {
      css: base$2
    },
    hintView: {
      css: base$3
    },
    hintSize: {
      css: base$4
    },
    disabled: {
      css: base$5,
      attrs: true
    },
    readOnly: {
      css: base$6,
      attrs: true
    }
  },
  defaults: {
    size: 'm',
    view: 'default',
    hintView: 'default',
    hintSize: 'm'
  }
};

export { rangeConfig, rangeRoot };
