"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateRangeSelector = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var useDidUpdateEffect_1 = require("../../../hooks/useDidUpdateEffect");
var DateIntervalSelector_1 = require("../../../DateIntervalSelector");
var CustomDateRangeEditor_1 = require("../../../CustomDateRangeEditor");
var SimpleDropDownSelector_1 = require("../../../SimpleDropDownSelector");
var helpers_1 = require("./helpers");
var styles_1 = require("./styles");
var emptyDates = [null, null];
var emptyInterval = [null, 'hours'];
var DateRangeSelector = function (_a) {
    var value = _a.value, onChange = _a.onChange, className = _a.className;
    var styles = (0, styles_1.useStyles)();
    var isIntervalType = function (type) { return type === mdm_sdk_1.DateRangeTypes.WITHIN || type === mdm_sdk_1.DateRangeTypes.AGO; };
    var isBetweenType = function (type) { return type === mdm_sdk_1.DateRangeTypes.BETWEEN; };
    var initDatesInterval = isBetweenType(value === null || value === void 0 ? void 0 : value.type) ? value.period : emptyDates;
    var initInterval = isIntervalType(value === null || value === void 0 ? void 0 : value.type) ? value.period : emptyInterval;
    var _b = (0, react_1.useState)(initDatesInterval), datesInterval = _b[0], setDatesInterval = _b[1];
    var _c = (0, react_1.useState)(initInterval), interval = _c[0], setInterval = _c[1];
    var selectedType = (value === null || value === void 0 ? void 0 : value.type) || mdm_sdk_1.DateRangeTypes.WITHIN;
    var getCurrentPeriodByType = function (type) {
        if (isIntervalType(type)) {
            return interval;
        }
        else if (isBetweenType(type)) {
            return datesInterval;
        }
    };
    var changeType = function (_a) {
        var type = _a.value;
        if ((value === null || value === void 0 ? void 0 : value.type) !== type) {
            onChange({ period: getCurrentPeriodByType(type), type: type });
        }
    };
    var changePeriod = function (period) {
        onChange({ type: selectedType, period: period });
    };
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        if (value) {
            if (isIntervalType(value.type)) {
                setInterval(value.period);
            }
            else if (isBetweenType(value.type)) {
                setDatesInterval(value.period);
            }
        }
        else {
            setDatesInterval(emptyDates);
            setInterval(emptyInterval);
        }
    }, [value]);
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)(styles.root, className), "data-reltio-id": "date-range-selector" },
        react_1.default.createElement(Typography_1.default, { className: styles.title, variant: 'subtitle1' }, ui_i18n_1.default.text('Date')),
        react_1.default.createElement("div", { className: styles.intervalContainer },
            react_1.default.createElement(SimpleDropDownSelector_1.SimpleDropDownSelector, { label: '', className: styles.typeInput, value: {
                    value: selectedType,
                    label: (0, helpers_1.getDateRangeTypeLabel)(selectedType)
                }, options: helpers_1.dateRangeTypeOptions.map(function (option) { return ({
                    value: option,
                    label: (0, helpers_1.getDateRangeTypeLabel)(option)
                }); }), onChange: changeType }),
            isIntervalType(selectedType) ? (react_1.default.createElement(DateIntervalSelector_1.DateIntervalSelector, { interval: interval, onChange: changePeriod })) : (react_1.default.createElement(CustomDateRangeEditor_1.CustomDateRangeEditor, { values: datesInterval, onChange: changePeriod })))));
};
exports.DateRangeSelector = DateRangeSelector;
