import Table, {
    ColumnsType as AntColumnsType,
    TableProps,
} from 'antd/es/table';
import React from 'react';
import StorageHelper from "@/lib/storeHelper";
import {STORAGE_KEYS} from "@/types/common";
import jaJP from 'antd/lib/locale/ja_JP';
import enUS from 'antd/lib/locale/en_US';
import {useTranslations} from "use-intl";

export interface RowActionConfig<T> {
    onClick?: (record: T) => void;
    onDoubleClick?: (record: T) => void;
    className?: string;
    style?: React.CSSProperties;
}

export interface ColumnSearchProps {
    searchable?: boolean;
}

// Re-export ColumnsType
export type ColumnsType<T> = AntColumnsType<T>;

type AppTableProps<T> = {
    columns: ColumnsType<T>;
    dataSource: T[];
    loading?: boolean;
    pagination?: TableProps<T>['pagination'];
    rowKey?: string | ((record: T) => string);
    scroll?: TableProps<T>['scroll'];
    onChange?: TableProps<T>['onChange'];
    rowConfig?: RowActionConfig<T>;
};

function AppTable<T extends object = any>({
                                              columns,
                                              dataSource,
                                              loading = false,
                                              pagination,
                                              rowKey,
                                              scroll,
                                              onChange,
                                              rowConfig,
                                          }: AppTableProps<T>) {

    const t = useTranslations("Common");
    const getRowProps = (record: T) => ({
        onClick: () => rowConfig?.onClick?.(record),
        onDoubleClick: () => rowConfig?.onDoubleClick?.(record),
        className: rowConfig?.className,
        style: {
            cursor: rowConfig?.onClick ? 'pointer' : 'default',
            ...rowConfig?.style
        }
    });
    return (
        <Table<T>
            locale={{
                filterReset: t("button.reset"),
                filterConfirm: t("button.ok"),
            }}
            columns={columns}
            dataSource={dataSource}
            loading={loading}
            pagination={pagination}
            rowKey={rowKey}
            scroll={scroll}
            onChange={onChange}
            onRow={getRowProps}
        />
    );
}

export default AppTable;
