"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "withRange", {
    enumerable: true,
    get: function() {
        return withRange;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _utils = require("../utils");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var withRange = function withRange(Component) {
    return function(_0) {
        var value = _0.value, min = _0.min, max = _0.max, disabledList = _0.disabledList, eventList = _0.eventList, eventMonthList = _0.eventMonthList, disabledMonthList = _0.disabledMonthList, eventYearList = _0.eventYearList, disabledYearList = _0.disabledYearList, onChangeValue = _0.onChangeValue, onChangeStartOfRange = _0.onChangeStartOfRange, rest = _object_without_properties(_0, [
            "value",
            "min",
            "max",
            "disabledList",
            "eventList",
            "eventMonthList",
            "disabledMonthList",
            "eventYearList",
            "disabledYearList",
            "onChangeValue",
            "onChangeStartOfRange"
        ]);
        var _useMemo = _sliced_to_array((0, _react.useMemo)(function() {
            return value;
        }, [
            value
        ]), 2), startExternalValue = _useMemo[0], endExternalValue = _useMemo[1];
        var _useState = _sliced_to_array((0, _react.useState)([
            startExternalValue,
            endExternalValue
        ]), 2), _useState_ = _sliced_to_array(_useState[0], 2), startValue = _useState_[0], endValue = _useState_[1], setValues = _useState[1];
        var _useState1 = _sliced_to_array((0, _react.useState)(value), 2), prevValue = _useState1[0], setPrevValue = _useState1[1];
        if ((0, _utils.isValueUpdate)(value, prevValue)) {
            setValues([
                startExternalValue,
                endExternalValue
            ]);
            setPrevValue(value);
        }
        var handleOnChangeDay = (0, _react.useCallback)(function(newDay, dateInfo) {
            if (endValue) {
                setValues([
                    newDay,
                    undefined
                ]);
                onChangeStartOfRange === null || onChangeStartOfRange === void 0 ? void 0 : onChangeStartOfRange(newDay, dateInfo);
                return;
            }
            setValues([
                startValue,
                newDay
            ]);
            var _getSortedValues = _sliced_to_array((0, _utils.getSortedValues)([
                startValue,
                newDay
            ]), 2), first = _getSortedValues[0], second = _getSortedValues[1];
            if (first) {
                onChangeValue([
                    first,
                    second
                ], dateInfo);
            }
        }, [
            onChangeValue,
            onChangeStartOfRange,
            startValue,
            endValue
        ]);
        (0, _react.useEffect)(function() {
            setValues(value);
        }, [
            value[0],
            value[1]
        ]);
        return /*#__PURE__*/ _react.default.createElement(Component, _object_spread({
            value: [
                startValue,
                endValue
            ],
            onChangeValue: handleOnChangeDay,
            disabledList: disabledList,
            eventList: eventList,
            eventMonthList: eventMonthList,
            disabledMonthList: disabledMonthList,
            eventYearList: eventYearList,
            disabledYearList: disabledYearList,
            min: min,
            max: max
        }, rest));
    };
};
