/**
 * Copyright (c) 2023-present Pi Dash Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import type { ColumnDef, Row, Table } from "@tanstack/react-table";
import { Download } from "lucide-react";
import { useTranslation } from "@pi-dash/i18n";
import { Button } from "@pi-dash/propel/button";
import type { AnalyticsTableDataMap, TAnalyticsTabsBase } from "@pi-dash/types";
import { DataTable } from "./data-table";
import { TableLoader } from "./loader";
interface InsightTableProps<T extends Exclude<TAnalyticsTabsBase, "overview">> {
  analyticsType: T;
  data?: AnalyticsTableDataMap[T][];
  isLoading?: boolean;
  columns: ColumnDef<AnalyticsTableDataMap[T]>[];
  columnsLabels?: Record<string, string>;
  headerText: string;
  onExport?: (rows: Row<AnalyticsTableDataMap[T]>[]) => void;
}

export function InsightTable<T extends Exclude<TAnalyticsTabsBase, "overview">>(
  props: InsightTableProps<T>
): React.ReactElement {
  const { data, isLoading, columns, headerText, onExport } = props;
  const { t } = useTranslation();
  if (isLoading) {
    return <TableLoader columns={columns} rows={5} />;
  }

  return (
    <div className="">
      <DataTable
        columns={columns}
        data={data || []}
        searchPlaceholder={`${data?.length || 0} ${headerText}`}
        actions={(table: Table<AnalyticsTableDataMap[T]>) => (
          <Button
            variant="secondary"
            prependIcon={<Download className="h-3.5 w-3.5" />}
            onClick={() => onExport?.(table.getFilteredRowModel().rows)}
          >
            <div>{t("Export as csv")}</div>
          </Button>
        )}
      />
    </div>
  );
}
