import React, { useState } from 'react';
import { PHXBadge } from '../../../Badge';
import { PHXText } from '../../../Text/Text';
import { ERoleCode, TABLE_CONSTANTS, EMPTY_VALUE } from '../../constant';
import { getRoleNameByTarget } from '../../ultis/get-role-name-by-target';
import { PHXTableReport } from '../../../TableReport';
import { renderParentCellTooltip } from '../../ultis/render-parent-cell-tooltip';
export const TableCustomUser = ({ data = [], invalidData, isInvalid, target }) => {
    const [currentPage, setCurrentPage] = useState(1);
    const [pageSize, setPageSize] = useState(10);
    const start = (currentPage - 1) * pageSize;
    const defaultHeader = ['Số điện thoại', 'Email'];
    const roleName = getRoleNameByTarget(target);
    const headerTable = {
        [ERoleCode.TEACHER]: ['Giáo viên', 'Mã giáo viên', ...defaultHeader],
        [ERoleCode.STAFF]: ['Nhân viên', 'Mã nhân viên', ...defaultHeader],
        [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'],
        [ERoleCode.DEPARTMENT]: ['Phòng ban', 'Mã phòng ban'],
        [ERoleCode.PARENT]: [...defaultHeader],
    };
    const defaultBody = ['phone', 'email', 'actions'];
    const bodyTable = {
        [ERoleCode.TEACHER]: [TABLE_CONSTANTS.STT_KEY, 'name', 'code', ...defaultBody],
        [ERoleCode.STAFF]: [TABLE_CONSTANTS.STT_KEY, 'name', 'code', ...defaultBody],
        [ERoleCode.STUDENT]: [
            TABLE_CONSTANTS.STT_KEY,
            'name',
            'code',
            'educational_level',
            'grade',
            'classroom',
            'parents',
        ],
        [ERoleCode.DEPARTMENT]: ['Phòng ban', 'Mã phòng ban', ...defaultHeader],
        [ERoleCode.PARENT]: [...defaultBody],
    };
    const createHeader = (names) => [
        {
            name: TABLE_CONSTANTS.STT,
            col: TABLE_CONSTANTS.COL,
            row: TABLE_CONSTANTS.ROW,
        },
        ...names.map((name) => ({
            name,
            col: 2,
            row: 1,
        })),
    ];
    const textCell = (value) => ({
        value: React.createElement(PHXText, null, value),
        row: 1,
        col: 2,
    });
    const sttColumn = (value) => ({
        ...textCell(value),
        row: TABLE_CONSTANTS.ROW,
        col: TABLE_CONSTANTS.COL,
    });
    const thComponent = {
        stt: sttColumn,
        phone: (val) => textCell(val || EMPTY_VALUE),
        email: (val) => textCell(val || EMPTY_VALUE),
        ...(target === ERoleCode.STUDENT && {
            parents: (val) => ({
                ...textCell(''),
                value: 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.createElement("div", { className: 'flex flex-col gap-1' },
                React.createElement(PHXBadge, { text: value || '', type: 'danger' }),
                React.createElement(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.createElement(PHXTableReport, { border: false, pagination: paginationConfig, readonly: true, tableData: paginatedData, textCenter: false, thBody: isShowInvalidForTable ? invalidBody : bodyTable[target], thBodyComponent: isShowInvalidForTable ? invalidThComponent : thComponent, thHeader: isShowInvalidForTable ? thInvalidHeader : thHeader }));
};
//# sourceMappingURL=table-custom-user.js.map