"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 textFieldSliderConfig () {
        return textFieldSliderConfig;
    },
    get textFieldSliderRoot () {
        return textFieldSliderRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _NumberFormat = require("../NumberFormat");
const _ui = require("./ui");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _base2 = require("./variations/_slider-view/base");
const _base3 = require("./variations/_hint-view/base");
const _base4 = require("./variations/_hint-size/base");
const _base5 = require("./variations/_disabled/base");
const _base6 = require("./variations/_readonly/base");
const _TextFieldSliderstyles = require("./TextFieldSlider.styles");
const _utils = require("./utils");
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 HINT_DEFAULT_OFFSET = [
    0,
    0
];
const textFieldSliderRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ id, className, style, // layout
    hasPointer = true, hasScale, scaleTicks, titleCaption, contentLeft, contentRight, textBefore, textAfter, // required
    required, requiredPlacement = 'right', optional, optionalText, hasRequiredIndicator = false, // hint
    hintTrigger = 'hover', hintText, hintView = 'default', hintSize = 'm', hintTargetIcon, hintTargetPlacement = 'outer', hintPlacement = 'auto', hintHasArrow, hintOffset = HINT_DEFAULT_OFFSET, hintWidth, hintContentLeft, hintPortal, // variations
    view, sliderView = 'default', size, readOnly = false, disabled = false, // controlled
    value: outerValue, defaultValue, min, max, step, decimalScale = 2, thousandsGroupStyle = 'none', thousandSeparator = '', decimalSeparator = '.', fixedDecimalScale = false, // events
    onChange, onChangeSlider, onChangeTextField, ...rest }, ref)=>{
        const [innerValue, setInnerValue] = (0, _react.useState)(defaultValue !== undefined ? defaultValue : '');
        const value = outerValue !== undefined ? outerValue : innerValue;
        const formatterArgs = (0, _react.useMemo)(()=>(0, _utils.createFormatterArgs)({
                decimalScale,
                thousandsGroupStyle,
                thousandSeparator,
                decimalSeparator,
                fixedDecimalScale
            }), [
            decimalScale,
            thousandsGroupStyle,
            thousandSeparator,
            decimalSeparator,
            fixedDecimalScale
        ]);
        const formattedMin = (0, _react.useMemo)(()=>(0, _NumberFormat.numberFormatter)(String(min) || '', formatterArgs), [
            min,
            formatterArgs
        ]);
        const formattedMax = (0, _react.useMemo)(()=>(0, _NumberFormat.numberFormatter)(String(max) || '', formatterArgs), [
            max,
            formatterArgs
        ]);
        const handleTickClick = (tick)=>{
            if (disabled || readOnly) return;
            const clamped = Math.max(min, Math.min(max, tick));
            setInnerValue(clamped);
            const formatted = (0, _NumberFormat.numberFormatter)(String(clamped), formatterArgs);
            if (onChange) {
                onChange(undefined, {
                    raw: clamped,
                    formatted
                });
            }
            if (onChangeSlider) {
                onChangeSlider({
                    raw: clamped,
                    formatted
                });
            }
        };
        return /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            disabled: disabled,
            readOnly: !disabled && readOnly,
            "data-root": true,
            className: className,
            style: style,
            sliderView: sliderView,
            ...hintText && {
                hintView,
                hintSize
            }
        }, /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.SliderWithHelpersWrapper, null, /*#__PURE__*/ _react.default.createElement(_ui.Input, {
            ref: ref,
            id: id,
            value: value,
            min: min,
            max: max,
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale,
            disabled: disabled,
            readOnly: readOnly,
            titleCaption: titleCaption,
            contentLeft: contentLeft,
            contentRight: contentRight,
            textBefore: textBefore,
            textAfter: textAfter,
            required: required,
            requiredPlacement: requiredPlacement,
            optional: optional,
            optionalText: optionalText,
            hasRequiredIndicator: hasRequiredIndicator,
            ...hintText ? {
                hintText,
                hintView,
                hintSize,
                hintTrigger,
                hintTargetIcon,
                hintPlacement,
                hintHasArrow,
                hintOffset,
                hintWidth,
                hintContentLeft,
                hintPortal,
                hintTargetPlacement
            } : {
                hintText: undefined
            },
            setInnerValue: setInnerValue,
            onChange: onChange,
            onChangeTextField: onChangeTextField,
            ...rest
        }), /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.SliderContainer, null, /*#__PURE__*/ _react.default.createElement(_ui.SliderUI, {
            value: value,
            min: min,
            max: max,
            step: step,
            decimalScale: decimalScale,
            thousandsGroupStyle: thousandsGroupStyle,
            thousandSeparator: thousandSeparator,
            decimalSeparator: decimalSeparator,
            fixedDecimalScale: fixedDecimalScale,
            disabled: disabled,
            readOnly: readOnly,
            hasPointer: hasPointer,
            onChange: onChange,
            onChangeSlider: onChangeSlider,
            setInnerValue: setInnerValue
        }))), hasScale && !scaleTicks && /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleWrapper, null, /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleValue, null, formattedMin), /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleValue, null, formattedMax)), scaleTicks && /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleTicksWrapper, null, scaleTicks.map((tick)=>{
            const percent = Math.max(0, Math.min(100, (tick - min) / (max - min) * 100));
            const numericValue = Number(value) || min;
            return /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleTick, {
                key: tick,
                style: {
                    left: `${percent}%`
                },
                onClick: ()=>handleTickClick(tick)
            }, /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleTickDot, {
                filled: tick <= numericValue
            }), /*#__PURE__*/ _react.default.createElement(_TextFieldSliderstyles.ScaleTickLabel, null, (0, _NumberFormat.numberFormatter)(String(tick), formatterArgs)));
        })));
    });
const textFieldSliderConfig = {
    name: 'TextFieldSlider',
    tag: 'div',
    layout: textFieldSliderRoot,
    base: _TextFieldSliderstyles.base,
    variations: {
        view: {
            css: _base1.base
        },
        size: {
            css: _base.base
        },
        disabled: {
            css: _base5.base,
            attrs: true
        },
        readOnly: {
            css: _base6.base,
            attrs: true
        },
        sliderView: {
            css: _base2.base
        },
        hintView: {
            css: _base3.base
        },
        hintSize: {
            css: _base4.base
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        sliderView: 'default'
    }
};
