import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  getDeployment,
  getDeploymentPipelineDescription,
  publishDeployment
} from "./chunk-J3APDAIV.js";
import {
  DeploymentPipelinesStatus,
  TaskStatus
} from "./chunk-MYWHJYA3.js";
import {
  useHttpClient
} from "./chunk-BRX4HWJE.js";
import {
  usePanoramaMethodLogger
} from "./chunk-ZBFOAJ5H.js";
import {
  pRetry,
  pWaitFor
} from "./chunk-YHFCDWGT.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import {
  getTestOverrides
} from "./chunk-XZUGLEBK.js";
import {
  external_exports
} from "./chunk-SDLCFZRL.js";
import {
  CliError,
  CliErrorCode,
  require_lib
} from "./chunk-WGV2XQSB.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../document-management-client/src/document-management-client.ts
init_esm_shims();
var import_variant = __toESM(require_lib(), 1);

// ../document-management-client/src/schemas.ts
init_esm_shims();
var getDeploymentSchema = external_exports.object({
  deployment: external_exports.object({
    deploymentPipelinesStatus: external_exports.enum([
      DeploymentPipelinesStatus.IN_PROGRESS,
      DeploymentPipelinesStatus.SUCCESS,
      DeploymentPipelinesStatus.ERROR
    ])
  })
});
var taskDescriptionSchema = external_exports.object({
  taskName: external_exports.string(),
  status: external_exports.string().transform(
    (value) => value === TaskStatus.SUCCESS ? TaskStatus.SUCCESS : TaskStatus.ERROR
  ),
  description: external_exports.string().nullish().transform((value, ctx) => {
    try {
      return value ? JSON.parse(value) : {};
    } catch {
      ctx.addIssue({
        code: external_exports.ZodIssueCode.custom,
        message: "Invalid JSON"
      });
    }
    return value;
  }).pipe(external_exports.record(external_exports.string(), external_exports.string()))
});
var getDeploymentPipelineDescriptionSchema = external_exports.object({
  pipelinesDescription: external_exports.array(
    external_exports.object({
      deploymentPipelineId: external_exports.string(),
      tasksDescriptions: external_exports.array(taskDescriptionSchema)
    })
  )
});

// ../document-management-client/src/document-management-client.ts
var { minRetryTimeout, pollInterval } = getTestOverrides();
var VELO_PIPELINE_ID = "5c696513-2584-4f19-ae0f-a559dd649482";
var DeploymentStatus = (0, import_variant.variant)({
  Success: {},
  Error: (0, import_variant.fields)()
});
var DocumentManagementClient = class {
  constructor(httpClient) {
    this.httpClient = httpClient;
    this.retryOptions = {
      retries: 1,
      minTimeout: minRetryTimeout,
      maxTimeout: 3 * 1e3
    };
  }
  retryOptions;
  getDeploymentDetails = async (deploymentId) => {
    const deploymentStatus = await this.getDeploymentStatus(deploymentId);
    if (deploymentStatus === DeploymentPipelinesStatus.ERROR) {
      const tasksDescriptions = await this.getDeploymentTasksDescriptions(deploymentId);
      return DeploymentStatus.Error({
        deploymentError: new CliError({
          code: CliErrorCode.DeploymentPipelineFailed({ tasksDescriptions }),
          cause: null
        })
      });
    }
    return DeploymentStatus.Success();
  };
  getDeploymentStatus = async (deploymentId) => {
    try {
      return await pWaitFor(
        async () => {
          const { data } = await pRetry(
            () => this.httpClient.request(getDeployment({ deploymentId })),
            this.retryOptions
          );
          const result = getDeploymentSchema.safeParse(data);
          if (!result.success) {
            throw new CliError({
              code: CliErrorCode.InvalidGetDeploymentResponse(),
              cause: result.error,
              info: { data }
            });
          }
          const { deploymentPipelinesStatus: status } = result.data.deployment;
          if (status === DeploymentPipelinesStatus.IN_PROGRESS) {
            return false;
          }
          return pWaitFor.resolveWith(status);
        },
        {
          // Poll each 2 seconds
          interval: pollInterval ?? 2 * 1e3,
          // Fail if 2 minutes passed
          timeout: 2 * 60 * 1e3
        }
      );
    } catch (error) {
      throw new CliError({
        code: CliErrorCode.FailedToGetDeploymentStatus(),
        cause: error
      });
    }
  };
  getDeploymentTasksDescriptions = async (deploymentId) => {
    try {
      const { data } = await pRetry(
        () => this.httpClient.request(
          getDeploymentPipelineDescription({
            deploymentId,
            deploymentPipelinesIds: [VELO_PIPELINE_ID]
          })
        ),
        this.retryOptions
      );
      const result = getDeploymentPipelineDescriptionSchema.safeParse(data);
      if (!result.success) {
        throw new CliError({
          code: CliErrorCode.InvalidGetDeploymentPipelineDescriptionResponse(),
          cause: result.error,
          info: { data }
        });
      }
      const veloPipeline = result.data.pipelinesDescription.find(
        (item) => item.deploymentPipelineId === VELO_PIPELINE_ID
      );
      return veloPipeline?.tasksDescriptions ?? [];
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToGetDeploymentPipelineDescription(),
        cause: e
      });
    }
  };
  publishDeployment = async (deploymentId) => {
    try {
      await this.httpClient.request(
        publishDeployment({ deploymentId, skipPipelineCheck: true })
      );
    } catch (error) {
      throw new CliError({
        code: CliErrorCode.FailedToPublishDeployment(),
        cause: error
      });
    }
  };
};

// ../document-management-client/src/useDocumentManagementClient.ts
init_esm_shims();
var import_react = __toESM(require_react(), 1);
function useDocumentManagementClient() {
  const httpClient = useHttpClient({ type: "editor" });
  const client = (0, import_react.useMemo)(
    () => new DocumentManagementClient(httpClient),
    [httpClient]
  );
  return usePanoramaMethodLogger(client);
}

// ../document-management-client/src/index.ts
init_esm_shims();

export {
  DeploymentStatus,
  useDocumentManagementClient
};
//# sourceMappingURL=chunk-Y6GHMYGK.js.map