import { Button } from '@chakra-ui/react';
import Papa from 'papaparse';
import { EVENT_NAMES } from '../../constants/define';

export default function ExportButton({ data, filename }) {
  const handleExport = async () => {
    const csv = Papa.unparse(JSON.stringify(data), {
      delimiter: ';',
    });
    await window.common[EVENT_NAMES.COMMON.SAVE_CSV_FILE]({ filename, csv });
  };

  return (
    <Button colorScheme="primary" onClick={handleExport}>
      Export to CSV
    </Button>
  );
}
