"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var FormControlLabel_1 = __importDefault(require("@mui/material/FormControlLabel"));
var Radio_1 = __importDefault(require("@mui/material/Radio"));
var RadioGroup_1 = __importDefault(require("@mui/material/RadioGroup"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var DateIntervalSelector_1 = require("../DateIntervalSelector");
var CustomDateRangeEditor_1 = require("../CustomDateRangeEditor");
var useDidUpdateEffect_1 = require("../hooks/useDidUpdateEffect");
var events_1 = require("../helpers/events");
var styles_1 = require("./styles");
var emptyDates = [null, null];
var emptyInterval = [null, 'minutes'];
var DateRangeSelector = function (_a) {
    var value = _a.value, onChange = _a.onChange, dateRangeTypes = _a.dateRangeTypes;
    var styles = (0, styles_1.useStyles)();
    var getInitPeriodByType = function (type) {
        if ((value === null || value === void 0 ? void 0 : value.type) === type) {
            return value.period;
        }
        else if (type === mdm_sdk_1.DateRangeTypes.WITHIN || type === mdm_sdk_1.DateRangeTypes.AGO) {
            return emptyInterval;
        }
        else if (type === mdm_sdk_1.DateRangeTypes.BETWEEN) {
            return emptyDates;
        }
    };
    var _b = (0, react_1.useState)(getInitPeriodByType(mdm_sdk_1.DateRangeTypes.BETWEEN)), datesInterval = _b[0], setDatesInterval = _b[1];
    var _c = (0, react_1.useState)(getInitPeriodByType(mdm_sdk_1.DateRangeTypes.WITHIN)), withinInterval = _c[0], setWithinInterval = _c[1];
    var _d = (0, react_1.useState)(getInitPeriodByType(mdm_sdk_1.DateRangeTypes.AGO)), agoInterval = _d[0], setAgoInterval = _d[1];
    var getCurrentPeriodByType = function (type) {
        if (type === mdm_sdk_1.DateRangeTypes.WITHIN) {
            return withinInterval;
        }
        else if (type === mdm_sdk_1.DateRangeTypes.AGO) {
            return agoInterval;
        }
        else if (type === mdm_sdk_1.DateRangeTypes.BETWEEN) {
            return datesInterval;
        }
    };
    var changeType = function (type) {
        if ((value === null || value === void 0 ? void 0 : value.type) !== type) {
            onChange({ period: getCurrentPeriodByType(type), type: type });
        }
    };
    var changePeriod = function (period) {
        onChange(__assign(__assign({}, value), { period: period }));
    };
    var isEditorVisible = function (_a) {
        var type = _a.type;
        return !dateRangeTypes || dateRangeTypes.includes(type);
    };
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        if (value) {
            if (value.type === mdm_sdk_1.DateRangeTypes.WITHIN) {
                setWithinInterval(value.period);
            }
            else if (value.type === mdm_sdk_1.DateRangeTypes.AGO) {
                setAgoInterval(value.period);
            }
            else if (value.type === mdm_sdk_1.DateRangeTypes.BETWEEN) {
                setDatesInterval(value.period);
            }
        }
        else {
            setDatesInterval(emptyDates);
            setWithinInterval(emptyInterval);
            setAgoInterval(emptyInterval);
        }
    }, [value]);
    var editors = [
        {
            label: ui_i18n_1.default.text('Within the last'),
            type: mdm_sdk_1.DateRangeTypes.WITHIN,
            editor: (react_1.default.createElement(DateIntervalSelector_1.DateIntervalSelector, { interval: withinInterval, onChange: changePeriod, onFocus: function () { return changeType(mdm_sdk_1.DateRangeTypes.WITHIN); } })),
            className: styles.dateOptionWithin
        },
        {
            label: ui_i18n_1.default.text('More than'),
            type: mdm_sdk_1.DateRangeTypes.AGO,
            editor: (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(DateIntervalSelector_1.DateIntervalSelector, { interval: agoInterval, onChange: changePeriod, onFocus: function () { return changeType(mdm_sdk_1.DateRangeTypes.AGO); } }),
                react_1.default.createElement(Typography_1.default, { className: styles.agoLabel, variant: 'subtitle1' }, ui_i18n_1.default.text('Ago')))),
            className: styles.dateOptionAgo
        },
        {
            label: ui_i18n_1.default.text('Date range'),
            type: mdm_sdk_1.DateRangeTypes.BETWEEN,
            editor: (react_1.default.createElement(CustomDateRangeEditor_1.CustomDateRangeEditor, { values: datesInterval, onChange: changePeriod, onFocus: function () { return changeType(mdm_sdk_1.DateRangeTypes.BETWEEN); } })),
            className: styles.dateOption
        }
    ];
    return (react_1.default.createElement(RadioGroup_1.default, { value: (value === null || value === void 0 ? void 0 : value.type) || null, onChange: (0, ramda_1.pipe)(events_1.getValue, changeType), "data-reltio-id": "reltio-date-range-selector" }, editors.filter(isEditorVisible).map(function (_a) {
        var type = _a.type, editor = _a.editor, label = _a.label, className = _a.className;
        return (react_1.default.createElement("div", { key: type, className: className },
            react_1.default.createElement(FormControlLabel_1.default, { value: type, label: label, control: react_1.default.createElement(Radio_1.default, { color: "primary" }), classes: { label: (0, classnames_1.default)(styles.dateOptionLabel, styles.labels) } }),
            editor));
    })));
};
exports.DateRangeSelector = DateRangeSelector;
