import { createFileWithPath, download, downloadTable, FileCollection, FileMimeType, renameDuplicatedFileName } from '@data-wrangling-components/utilities';
import { useCallback, useState } from 'react';
export function useDownloadWorkflow(workflow) {
    return useCallback(()=>{
        if (workflow != null) {
            const blob = toBlobJson(workflow.toJsonObject());
            download('worfklow.json', FileMimeType.json, blob);
        }
    }, [
        workflow
    ]);
}
export function useDownloadCsv(tables) {
    return useCallback(()=>{
        for (const table of tables){
            if (table?.table) {
                downloadTable(table.table, tableName(table.name || table.id));
            }
        }
    }, [
        tables
    ]);
}
export function useDownloadZip(workflow, tables, outputTables) {
    const [fileCollection] = useState(new FileCollection());
    return useCallback(async ()=>{
        fileCollection.clear();
        const input = [];
        const output = [];
        for (const outputTable of outputTables){
            if (outputTable?.table) {
                const file = tableFile(outputTable, outputTable.name || outputTable.id);
                if (file) {
                    await fileCollection.add(file);
                    output.push(file.name);
                }
            }
        }
        if (tables.length) {
            const map = new Map();
            const renamedTables = tables.map((t)=>({
                    ...t,
                    name: renameDuplicatedFileName(map, t.name || t.id || 'table.csv')
                }));
            const files = renamedTables.map((table)=>tableFile(table)).filter((f)=>f != null);
            if (files.length) {
                await fileCollection.add(files);
                tables.forEach((table)=>{
                    input.push(table.name || table.id);
                });
            }
        }
        if (input.length || output.length) {
            const blob = toBlobJson({
                input,
                output
            });
            const file = createFileWithPath(blob, {
                name: 'metadata.json'
            });
            await fileCollection.add(file);
        }
        if (workflow) {
            const blob = toBlobJson(workflow.toJsonObject());
            const file = createFileWithPath(blob, {
                name: 'workflow.json'
            });
            await fileCollection.add(file);
        }
        if (fileCollection.list().length) {
            await fileCollection.toZip('dwc-project');
        }
    }, [
        fileCollection,
        workflow,
        tables,
        outputTables
    ]);
}
function toBlobJson(serializable) {
    return new Blob([
        JSON.stringify(serializable, null, 4)
    ]);
}
function tableName(name) {
    return name.endsWith('.csv') ? name : `${name}.csv`;
}
function tableFile(t, name) {
    return t.table ? createFileWithPath(new Blob([
        t.table.toCSV()
    ]), {
        name: tableName(name || t.name || t.id)
    }) : null;
}
