"use strict";

exports.__esModule = true;
exports.createHubJobClient = createHubJobClient;
var _http = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/http");
var _types = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/types");
/**
 * Thin wrapper over the Hub Job (data-movement) API used by the server-side
 * import. Each call goes through `errorHandler.withErrorHandler` and the
 * container's `httpClient`, mirroring `ExportState`.
 *
 * Holds no observable state — it's a standalone unit consumed by `ImportState`.
 */

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((0, _http.createFileUploadUrl)({
        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 = _types.FileFormat.CSV,
      transformations
    }) {
      const {
        data: {
          job
        }
      } = await errorHandler.withErrorHandler(() => httpClient.request((0, _http.submitJob)({
        name,
        source: {
          file: {
            fileId,
            format: fileFormat
          }
        },
        destination,
        ...(transformations && {
          transformations
        })
      })), {});
      if (!(job != null && job.id)) {
        throw new Error('submitJob returned no job id');
      }
      return job.id;
    },
    async getJob(jobId) {
      const {
        data: {
          job
        }
      } = await errorHandler.withErrorHandler(() => httpClient.request((0, _http.getJob)({
        jobId
      })), {});
      if (!job) {
        throw new Error(`getJob returned no job for "${jobId}"`);
      }
      return job;
    },
    async terminateJob(jobId) {
      await errorHandler.withErrorHandler(() => httpClient.request((0, _http.terminateJob)({
        jobId
      })), {});
    },
    async listMovementLogs(jobId, paging) {
      const {
        data
      } = await errorHandler.withErrorHandler(() => httpClient.request((0, _http.listMovementLogs)({
        jobId,
        ...(paging && {
          paging
        })
      })), {});
      return data;
    }
  };
}
//# sourceMappingURL=hubJobClient.js.map