"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxGroup = CheckboxGroup;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const constant_1 = require("../constant");
const table_custom_list_1 = tslib_1.__importDefault(require("./table-custom-list"));
const BannerWithCard_1 = require("../../BannerWithCard/BannerWithCard");
const Combobox_1 = require("../../Combobox");
const Checkbox_1 = require("../../Checkbox");
function CheckboxGroup({ errors, handleChangeFilter, listRadio, listTargetSelected, schoolYearId, selectOptions, target, }) {
    const current = listTargetSelected.find((i) => i.target === target);
    const filters = (current === null || current === void 0 ? void 0 : current.filter) || [];
    const isDepartmentOrStaff = target === constant_1.ERoleCode.DEPARTMENT || target === constant_1.ERoleCode.STAFF;
    const isAllDepartmentChecked = filters.some((f) => f.filterBy === constant_1.FILTER_BY.ALL) && isDepartmentOrStaff;
    const isAllChecked = filters.some((f) => f.filterBy === constant_1.FILTER_BY.ALL && !isDepartmentOrStaff);
    (0, react_1.useEffect)(() => {
        if (isAllChecked) {
            const firstRadio = listRadio[0];
            if (!firstRadio || firstRadio.value === constant_1.FILTER_BY.CUSTOM_LIST)
                return;
            handleChangeFilter(target, true, {
                filterBy: firstRadio.value,
                filterValues: selectOptions[firstRadio.value],
            });
        }
    }, [isAllChecked]);
    return (react_1.default.createElement("div", { className: 'ml-7 space-y-2' }, listRadio.map((radio, index) => {
        var _a, _b, _c, _d, _e, _f;
        const { title, value } = radio;
        const existing = filters.find((f) => f.filterBy === value);
        const isChecked = !!existing;
        const isHaveCombobox = isChecked && value !== constant_1.FILTER_BY.CUSTOM_LIST && target !== constant_1.ERoleCode.STAFF && value !== constant_1.FILTER_BY.ALL;
        const haveError = !!((_b = (_a = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _a === void 0 ? void 0 : _a.filterValues) === null || _b === void 0 ? void 0 : _b.message);
        return (react_1.default.createElement("div", { key: value, className: 'space-y-2' },
            react_1.default.createElement(Checkbox_1.PHXCheckbox, { checked: isChecked, disabled: isAllDepartmentChecked && value !== constant_1.FILTER_BY.ALL, error: haveError, id: `${target}-${value}`, onChange: (val) => {
                    handleChangeFilter(target, val.target.checked, {
                        filterBy: value,
                        filterValues: [],
                    });
                }, title: title }),
            value === constant_1.FILTER_BY.CUSTOM_LIST && isChecked && (react_1.default.createElement("div", null,
                haveError && (react_1.default.createElement(BannerWithCard_1.PHXBannerWithCard, { className: 'mb-3 ml-6', description: (_d = (_c = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _c === void 0 ? void 0 : _c.filterValues) === null || _d === void 0 ? void 0 : _d.message, hideCloseButton: true, show: true, type: 'danger' })),
                react_1.default.createElement(table_custom_list_1.default, { defaultData: (existing === null || existing === void 0 ? void 0 : existing.filterValues) || [], handleChange: (data) => {
                        handleChangeFilter(target, true, {
                            filterBy: value,
                            filterValues: data,
                        });
                    }, schoolYearId: schoolYearId, target: target }))),
            isHaveCombobox && (react_1.default.createElement(Combobox_1.PHXCombobox, { className: 'ml-6', defaultValue: (existing === null || existing === void 0 ? void 0 : existing.filterValues) || [], error: haveError, errorMessageCustom: (_f = (_e = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _e === void 0 ? void 0 : _e.filterValues) === null || _f === void 0 ? void 0 : _f.message, errorType: 'custom-message', listOptions: selectOptions[value] || [], onChange: (selected) => handleChangeFilter(target, true, {
                    filterBy: value,
                    filterValues: selected,
                }), placeholder: 'L\u1EF1a ch\u1ECDn', selectFieldPosition: target === constant_1.ERoleCode.DEPARTMENT ? 'top' : 'bottom' }))));
    })));
}
//# sourceMappingURL=radio-group-select.js.map