/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { Workflow } from '@datashaper/workflow';
import { useCallback, useMemo } from 'react';
import { useDownloadCsv, useDownloadWorkflow, useDownloadZip } from '../hooks/downloads.js';
import { useHandleCsvUpload, useHandleJsonUpload, useHandleZipUpload } from '../hooks/uploads.js';
export function useProjectManagementCommands(workflow, tables, outputTables, itemProps = {}, onUpdateWorkflowJson, onUpdateTables, onError) {
    const downloadPipeline = useDownloadWorkflow(workflow);
    const downloadCsv = useDownloadCsv(outputTables);
    const downloadZip = useDownloadZip(workflow, tables, outputTables);
    const handleJsonUpload = useHandleJsonUpload(onUpdateWorkflowJson, onError);
    const handleCsvUpload = useHandleCsvUpload(onUpdateTables);
    const handleZipUpload = useHandleZipUpload(onUpdateWorkflowJson, onUpdateTables, onError);
    const commands = useMemo(()=>{
        return [
            {
                key: 'open',
                text: 'Open',
                iconProps: icons.openFile,
                ...itemProps,
                subMenuProps: {
                    items: [
                        {
                            key: 'csv',
                            text: 'CSV table',
                            iconProps: icons.table,
                            onClick: handleCsvUpload
                        },
                        {
                            key: 'json',
                            text: 'Pipeline (.json)',
                            iconProps: icons.code,
                            onClick: handleJsonUpload
                        },
                        {
                            key: 'zip',
                            text: 'Zip project',
                            iconProps: icons.zipFolder,
                            onClick: handleZipUpload
                        }
                    ]
                }
            },
            {
                key: 'save',
                text: 'Save',
                iconProps: icons.save,
                ...itemProps,
                subMenuProps: {
                    items: [
                        {
                            key: 'csv',
                            text: 'CSV table',
                            iconProps: icons.table,
                            onClick: downloadCsv
                        },
                        {
                            key: 'json',
                            text: 'Pipeline (.json)',
                            iconProps: icons.code,
                            onClick: downloadPipeline
                        },
                        {
                            key: 'zip',
                            text: 'Zip project',
                            iconProps: icons.zipFolder,
                            onClick: downloadZip
                        }
                    ]
                }
            }
        ];
    }, [
        itemProps,
        downloadPipeline,
        downloadCsv,
        downloadZip,
        handleJsonUpload,
        handleCsvUpload,
        handleZipUpload
    ]);
    return commands;
}
export function useOnUpdateWorkflowJson(onUpdateWorkflow) {
    return useCallback(async (wf)=>{
        const valid = await Workflow.validate(wf);
        if (!valid) throw Error('Invalid workflow definition');
        return onUpdateWorkflow(new Workflow(wf));
    }, [
        onUpdateWorkflow
    ]);
}
const icons = {
    save: {
        iconName: 'Save'
    },
    openFile: {
        iconName: 'OpenFile'
    },
    table: {
        iconName: 'Table'
    },
    code: {
        iconName: 'Code'
    },
    zipFolder: {
        iconName: 'ZipFolder'
    }
};
