"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "withRange", {
    enumerable: true,
    get: function() {
        return withRange;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
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 withRange = (Component)=>({ value, min, max, disabledList, eventList, eventMonthList, disabledMonthList, eventYearList, disabledYearList, onChangeValue, onChangeStartOfRange, onChangeSingleValue, ...rest })=>{
        const [startExternalValue, endExternalValue] = (0, _react.useMemo)(()=>value, [
            value
        ]);
        const [[startValue, endValue], setValues] = (0, _react.useState)([
            startExternalValue,
            endExternalValue
        ]);
        const [prevValue, setPrevValue] = (0, _react.useState)(value);
        if ((0, _utils.isValueUpdate)(value, prevValue)) {
            setValues([
                startExternalValue,
                endExternalValue
            ]);
            setPrevValue(value);
        }
        const handleOnChangeDay = (0, _react.useCallback)((newDay, dateInfo)=>{
            if (endValue) {
                setValues([
                    newDay,
                    undefined
                ]);
                onChangeStartOfRange?.(newDay, dateInfo);
                return;
            }
            if (startValue) {
                setValues([
                    startValue,
                    newDay
                ]);
                const [first, second] = (0, _utils.getSortedValues)([
                    startValue,
                    newDay
                ]);
                onChangeValue([
                    first,
                    second
                ], dateInfo);
            } else {
                setValues([
                    newDay,
                    undefined
                ]);
                onChangeSingleValue?.(newDay, dateInfo);
            }
        }, [
            onChangeValue,
            onChangeStartOfRange,
            onChangeSingleValue,
            startValue,
            endValue
        ]);
        (0, _react.useEffect)(()=>{
            setValues(value);
        }, [
            value[0],
            value[1]
        ]);
        return /*#__PURE__*/ _react.default.createElement(Component, {
            value: [
                startValue,
                endValue
            ],
            onChangeValue: handleOnChangeDay,
            disabledList: disabledList,
            eventList: eventList,
            eventMonthList: eventMonthList,
            disabledMonthList: disabledMonthList,
            eventYearList: eventYearList,
            disabledYearList: disabledYearList,
            min: min,
            max: max,
            ...rest
        });
    };
