import { submitJob as submitJobRequest, createFileUploadUrl as createFileUploadUrlRequest, getJob as getJobRequest, terminateJob as terminateJobRequest, listMovementLogs as listMovementLogsRequest, } from '@wix/bex-utils/@wix/ambassador-hub-v1-job/http';
import { FileFormat, } from '@wix/bex-utils/@wix/ambassador-hub-v1-job/types';
export function createHubJobClient({ httpClient, errorHandler, }) {
    // Empty error-code map = default handling. Specific codes/messages need
    // translation keys and are deferred to the translations task.
    return {
        async uploadFile(file) {
            const { data: { uploadUrl, fileId }, } = await errorHandler.withErrorHandler(() => httpClient.request(createFileUploadUrlRequest({ fileName: file.name })), {});
            if (!uploadUrl || !fileId) {
                throw new Error('createFileUploadUrl returned no uploadUrl/fileId');
            }
            await errorHandler.withErrorHandler(() => httpClient.request({ url: uploadUrl, method: 'PUT', data: file }), {});
            return fileId;
        },
        async submitImportJob({ fileId, destination, name, fileFormat = FileFormat.CSV, transformations, }) {
            const { data: { job }, } = await errorHandler.withErrorHandler(() => httpClient.request(submitJobRequest({
                name,
                source: { file: { fileId, format: fileFormat } },
                destination,
                ...(transformations && { transformations }),
            })), {});
            if (!job?.id) {
                throw new Error('submitJob returned no job id');
            }
            return job.id;
        },
        async getJob(jobId) {
            const { data: { job }, } = await errorHandler.withErrorHandler(() => httpClient.request(getJobRequest({ jobId })), {});
            if (!job) {
                throw new Error(`getJob returned no job for "${jobId}"`);
            }
            return job;
        },
        async terminateJob(jobId) {
            await errorHandler.withErrorHandler(() => httpClient.request(terminateJobRequest({ jobId })), {});
        },
        async listMovementLogs(jobId, paging) {
            const { data } = await errorHandler.withErrorHandler(() => httpClient.request(listMovementLogsRequest({ jobId, ...(paging && { paging }) })), {});
            return data;
        },
    };
}
//# sourceMappingURL=hubJobClient.js.map