"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDateEditor = void 0;
var react_1 = require("react");
var moment_1 = __importDefault(require("moment"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var helpers_1 = require("./helpers");
var constants_1 = require("./constants");
var _a = mdm_sdk_1.utils.dates, isValidMomentDateOrNull = _a.isValidMomentDateOrNull, isDateInRange = _a.isDateInRange, momentToDate = _a.momentToDate;
var useDateEditor = function (_a) {
    var minDateProp = _a.minDate, maxDateProp = _a.maxDate, value = _a.value, onChange = _a.onChange;
    var _b = (0, react_1.useState)(null), error = _b[0], setError = _b[1];
    var _c = (0, react_1.useState)(null), editorValue = _c[0], setEditorValue = _c[1];
    var minDate = (0, react_1.useMemo)(function () { return mdm_sdk_1.utils.dates.clearDateTime((0, ramda_1.isNil)(minDateProp) ? constants_1.DEFAULT_MIN_DATE : minDateProp); }, [minDateProp]);
    var maxDate = (0, react_1.useMemo)(function () { return mdm_sdk_1.utils.dates.clearDateTime((0, ramda_1.isNil)(maxDateProp) ? constants_1.DEFAULT_MAX_DATE : maxDateProp); }, [maxDateProp]);
    var momentValue = (0, react_1.useMemo)(function () { return ((0, ramda_1.isNil)(value) ? null : (0, moment_1.default)(value)); }, [value]);
    var editorMinDate = (0, react_1.useMemo)(function () { return (0, helpers_1.getEditorMinDate)(minDate); }, [minDate]);
    var editorMaxDate = (0, react_1.useMemo)(function () { return (0, helpers_1.getEditorMaxDate)(maxDate); }, [maxDate]);
    var errorMessage = (0, react_1.useMemo)(function () { return (0, helpers_1.getErrorMessage)(error); }, [error]);
    var handleError = (0, react_1.useCallback)(function (error) {
        if (!(0, helpers_1.isDateRangeError)(error))
            setError(error);
    }, []);
    var handleChange = (0, react_1.useCallback)(function (value) {
        setEditorValue(value);
        if (isValidMomentDateOrNull(value) && isDateInRange(minDate, maxDate)(value)) {
            onChange(momentToDate(value));
        }
    }, [minDate, maxDate, onChange]);
    var debouncedHandleChange = (0, react_1.useCallback)((0, mdm_sdk_1.debounce)(handleChange, 0), [handleChange]);
    (0, react_1.useEffect)(function () {
        setEditorValue(function (prevEditorValue) {
            var datesAreTheSame = (0, moment_1.default)(prevEditorValue).isSame(momentValue);
            var datesAreInvalid = !isValidMomentDateOrNull(momentValue) && !isValidMomentDateOrNull(prevEditorValue);
            return datesAreTheSame || datesAreInvalid ? prevEditorValue : momentValue;
        });
    }, [momentValue]);
    (0, react_1.useEffect)(function () {
        if (editorValue === null || editorValue === void 0 ? void 0 : editorValue.isAfter(maxDate)) {
            setError('maxDate');
        }
        else if (editorValue === null || editorValue === void 0 ? void 0 : editorValue.isBefore(minDate)) {
            setError('minDate');
        }
        else {
            setError(function (prevError) { return ((0, helpers_1.isDateRangeError)(prevError) ? null : prevError); });
        }
    }, [editorValue, maxDate, minDate]);
    return {
        errorMessage: errorMessage,
        editorValue: editorValue,
        editorMinDate: editorMinDate,
        editorMaxDate: editorMaxDate,
        onChange: debouncedHandleChange,
        onError: handleError
    };
};
exports.useDateEditor = useDateEditor;
