import React, { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Namespaces } from "../../../../i18n/i18n";
import Column from "../../../../components/common/columns";
import Table, {
  PublishedTableProps,
} from "../../../../components/common/Table";
import authentication from "../../../frame/services/authenticationService";
import { ReadSpecification } from "../services/specificationService";

const SpecificationsTable: React.FC<PublishedTableProps<ReadSpecification>> = ({
  data,
  sortColumn,
  onChangePage,
  onSearch,
  onDelete,
  onSort,
}) => {
  const { t } = useTranslation<typeof Namespaces.Common>();

  const columns: Column<ReadSpecification>[] = useMemo(
    () => [{ key: "name", label: t("Name"), order: "asc" }],
    [t],
  );

  const raiseSort = (sortCol: Column<ReadSpecification>) => {
    if (onSort !== undefined) onSort(sortCol);
  };

  const handleAuditParams = (item: any) => {
    return {
      entityName: "e_suite.Database.Core.Tables.Printer.Specification",
      primaryKey: `{"Id":${item.id}}`,
    };
  };

  const editPath = authentication.hasAccess("EditSpecification")
    ? "{0}"
    : undefined;
  const doDelete = authentication.hasAccess("DeleteSpecification")
    ? onDelete
    : undefined;
  const showAudit = authentication.hasAccess("ViewAuditLog")
    ? handleAuditParams
    : undefined;

  return (
    <Table
      data={data}
      keyName="id"
      columns={columns}
      sortColumn={sortColumn}
      editPath={editPath}
      onSort={raiseSort}
      onChangePage={onChangePage}
      onSearch={onSearch}
      onDelete={doDelete}
      onAuditParams={showAudit}
    />
  );
};

export default SpecificationsTable;
