"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableCustomUser = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const Badge_1 = require("../../../Badge");
const Text_1 = require("../../../Text/Text");
const constant_1 = require("../../constant");
const get_role_name_by_target_1 = require("../../ultis/get-role-name-by-target");
const TableReport_1 = require("../../../TableReport");
const render_parent_cell_tooltip_1 = require("../../ultis/render-parent-cell-tooltip");
const TableCustomUser = ({ data = [], invalidData, isInvalid, target }) => {
    const [currentPage, setCurrentPage] = (0, react_1.useState)(1);
    const [pageSize, setPageSize] = (0, react_1.useState)(10);
    const start = (currentPage - 1) * pageSize;
    const defaultHeader = ['Số điện thoại', 'Email'];
    const roleName = (0, get_role_name_by_target_1.getRoleNameByTarget)(target);
    const headerTable = {
        [constant_1.ERoleCode.TEACHER]: ['Giáo viên', 'Mã giáo viên', ...defaultHeader],
        [constant_1.ERoleCode.STAFF]: ['Nhân viên', 'Mã nhân viên', ...defaultHeader],
        [constant_1.ERoleCode.STUDENT]: ['Học sinh', 'Mã học sinh', 'Cấp học', 'Khối học', 'Lớp học', 'Người thân'],
        [constant_1.ERoleCode.DEPARTMENT]: ['Phòng ban', 'Mã phòng ban'],
        [constant_1.ERoleCode.PARENT]: [...defaultHeader],
    };
    const defaultBody = ['phone', 'email', 'actions'];
    const bodyTable = {
        [constant_1.ERoleCode.TEACHER]: [constant_1.TABLE_CONSTANTS.STT_KEY, 'name', 'code', ...defaultBody],
        [constant_1.ERoleCode.STAFF]: [constant_1.TABLE_CONSTANTS.STT_KEY, 'name', 'code', ...defaultBody],
        [constant_1.ERoleCode.STUDENT]: [
            constant_1.TABLE_CONSTANTS.STT_KEY,
            'name',
            'code',
            'educational_level',
            'grade',
            'classroom',
            'parents',
        ],
        [constant_1.ERoleCode.DEPARTMENT]: ['Phòng ban', 'Mã phòng ban', ...defaultHeader],
        [constant_1.ERoleCode.PARENT]: [...defaultBody],
    };
    const createHeader = (names) => [
        {
            name: constant_1.TABLE_CONSTANTS.STT,
            col: constant_1.TABLE_CONSTANTS.COL,
            row: constant_1.TABLE_CONSTANTS.ROW,
        },
        ...names.map((name) => ({
            name,
            col: 2,
            row: 1,
        })),
    ];
    const textCell = (value) => ({
        value: react_1.default.createElement(Text_1.PHXText, null, value),
        row: 1,
        col: 2,
    });
    const sttColumn = (value) => ({
        ...textCell(value),
        row: constant_1.TABLE_CONSTANTS.ROW,
        col: constant_1.TABLE_CONSTANTS.COL,
    });
    const thComponent = {
        stt: sttColumn,
        phone: (val) => textCell(val || constant_1.EMPTY_VALUE),
        email: (val) => textCell(val || constant_1.EMPTY_VALUE),
        ...(target === constant_1.ERoleCode.STUDENT && {
            parents: (val) => ({
                ...textCell(''),
                value: (0, render_parent_cell_tooltip_1.renderParentCellTooltip)(val),
            }),
        }),
    };
    const thHeader = createHeader(headerTable[target]);
    const invalidHeader = [`Mã ${roleName}`, 'Lý do lỗi'];
    const thInvalidHeader = createHeader(invalidHeader);
    const invalidBody = ['stt', 'code', 'reason'];
    const invalidThComponent = {
        stt: sttColumn,
        code: textCell,
        reason: (value, record) => ({
            ...textCell(''),
            value: (react_1.default.createElement("div", { className: 'flex flex-col gap-1' },
                react_1.default.createElement(Badge_1.PHXBadge, { text: value || '', type: 'danger' }),
                react_1.default.createElement(Text_1.PHXText, { size: 'xs', tone: 'subdued' }, record.reasonDetail))),
        }),
    };
    const formatTableData = (list) => list.map((item, index) => ({
        ...item,
        stt: index + 1,
    }));
    const isShowInvalidForTable = !!(isInvalid && invalidData);
    const tableData = isShowInvalidForTable ? formatTableData(invalidData || []) : formatTableData(data);
    const paginatedData = tableData.slice(start, start + pageSize);
    const paginationConfig = {
        dataAggregate: tableData.length,
        enable: paginatedData.length > 0,
        rowNumberInPage: pageSize,
        onChange: setCurrentPage,
        setRowNumberInPage: setPageSize,
    };
    return (react_1.default.createElement(TableReport_1.PHXTableReport, { border: false, pagination: paginationConfig, readonly: true, tableData: paginatedData, textCenter: false, thBody: isShowInvalidForTable ? invalidBody : bodyTable[target], thBodyComponent: isShowInvalidForTable ? invalidThComponent : thComponent, thHeader: isShowInvalidForTable ? thInvalidHeader : thHeader }));
};
exports.TableCustomUser = TableCustomUser;
//# sourceMappingURL=table-custom-user.js.map