import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  listFiles,
  updateFiles
} from "./chunk-DLEBD2WZ.js";
import {
  AppType,
  DeploymentOperation,
  RcLabel,
  createApp,
  deployApp
} from "./chunk-RTNIG6ZW.js";
import {
  getResolveNpmDependenciesResult,
  resolveNpmDependencies
} from "./chunk-O6HC4AYN.js";
import {
  GridAppLayout
} from "./chunk-QRGKMYDD.js";
import {
  useHttpClient
} from "./chunk-WFQIXGJV.js";
import {
  usePanoramaMethodLogger
} from "./chunk-ECBO3UFC.js";
import {
  createHttpClient,
  isHttpApplicationError,
  isHttpError,
  pRetry,
  pWaitFor
} from "./chunk-HWT3LUJJ.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import {
  getTestOverrides
} from "./chunk-XZUGLEBK.js";
import {
  external_exports
} from "./chunk-SDLCFZRL.js";
import {
  CliError,
  CliErrorCode
} from "./chunk-MTT3QXBC.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../velo-client/src/useVeloClient.ts
init_esm_shims();
var import_react = __toESM(require_react(), 1);

// ../velo-client/src/velo-client.ts
init_esm_shims();

// ../../node_modules/@wix/ambassador-velo-github-v1-github-onboarding-status/build/es/types.impl.js
init_esm_shims();
var RepositoryOwnerType;
(function(RepositoryOwnerType2) {
  RepositoryOwnerType2["USER"] = "USER";
  RepositoryOwnerType2["ORGANIZATION"] = "ORGANIZATION";
})(RepositoryOwnerType || (RepositoryOwnerType = {}));
var Label;
(function(Label2) {
  Label2["UNKNOWN"] = "UNKNOWN";
  Label2["WIX_VIBE"] = "WIX_VIBE";
  Label2["DEVIBE"] = "DEVIBE";
})(Label || (Label = {}));
var OnboardingState;
(function(OnboardingState2) {
  OnboardingState2["GITHUB_DISABLED"] = "GITHUB_DISABLED";
  OnboardingState2["GITHUB_REPOSITORY_CREATED"] = "GITHUB_REPOSITORY_CREATED";
  OnboardingState2["GITHUB_ENABLED"] = "GITHUB_ENABLED";
})(OnboardingState || (OnboardingState = {}));
var State;
(function(State2) {
  State2["UNKNOWN"] = "UNKNOWN";
  State2["ENABLED"] = "ENABLED";
  State2["DISABLED"] = "DISABLED";
  State2["PENDING"] = "PENDING";
  State2["DEMO"] = "DEMO";
})(State || (State = {}));
var SiteCreatedContext;
(function(SiteCreatedContext2) {
  SiteCreatedContext2["OTHER"] = "OTHER";
  SiteCreatedContext2["FROM_TEMPLATE"] = "FROM_TEMPLATE";
  SiteCreatedContext2["DUPLICATE_BY_SITE_TRANSFER"] = "DUPLICATE_BY_SITE_TRANSFER";
  SiteCreatedContext2["DUPLICATE"] = "DUPLICATE";
  SiteCreatedContext2["OLD_SITE_TRANSFER"] = "OLD_SITE_TRANSFER";
  SiteCreatedContext2["FLASH"] = "FLASH";
})(SiteCreatedContext || (SiteCreatedContext = {}));
var Namespace;
(function(Namespace2) {
  Namespace2["UNKNOWN_NAMESPACE"] = "UNKNOWN_NAMESPACE";
  Namespace2["WIX"] = "WIX";
  Namespace2["SHOUT_OUT"] = "SHOUT_OUT";
  Namespace2["ALBUMS"] = "ALBUMS";
  Namespace2["WIX_STORES_TEST_DRIVE"] = "WIX_STORES_TEST_DRIVE";
  Namespace2["HOTELS"] = "HOTELS";
  Namespace2["CLUBS"] = "CLUBS";
  Namespace2["ONBOARDING_DRAFT"] = "ONBOARDING_DRAFT";
  Namespace2["DEV_SITE"] = "DEV_SITE";
  Namespace2["LOGOS"] = "LOGOS";
  Namespace2["VIDEO_MAKER"] = "VIDEO_MAKER";
  Namespace2["PARTNER_DASHBOARD"] = "PARTNER_DASHBOARD";
  Namespace2["DEV_CENTER_COMPANY"] = "DEV_CENTER_COMPANY";
  Namespace2["HTML_DRAFT"] = "HTML_DRAFT";
  Namespace2["SITELESS_BUSINESS"] = "SITELESS_BUSINESS";
  Namespace2["CREATOR_ECONOMY"] = "CREATOR_ECONOMY";
  Namespace2["DASHBOARD_FIRST"] = "DASHBOARD_FIRST";
  Namespace2["ANYWHERE"] = "ANYWHERE";
  Namespace2["HEADLESS"] = "HEADLESS";
  Namespace2["ACCOUNT_MASTER_CMS"] = "ACCOUNT_MASTER_CMS";
  Namespace2["RISE"] = "RISE";
  Namespace2["BRANDED_FIRST"] = "BRANDED_FIRST";
  Namespace2["NOWNIA"] = "NOWNIA";
  Namespace2["UGC_TEMPLATE"] = "UGC_TEMPLATE";
  Namespace2["CODUX"] = "CODUX";
  Namespace2["MEDIA_DESIGN_CREATOR"] = "MEDIA_DESIGN_CREATOR";
  Namespace2["SHARED_BLOG_ENTERPRISE"] = "SHARED_BLOG_ENTERPRISE";
  Namespace2["STANDALONE_FORMS"] = "STANDALONE_FORMS";
  Namespace2["STANDALONE_EVENTS"] = "STANDALONE_EVENTS";
  Namespace2["MIMIR"] = "MIMIR";
  Namespace2["TWINS"] = "TWINS";
  Namespace2["NANO"] = "NANO";
  Namespace2["BASE44"] = "BASE44";
})(Namespace || (Namespace = {}));
var DeleteStatus;
(function(DeleteStatus2) {
  DeleteStatus2["UNKNOWN"] = "UNKNOWN";
  DeleteStatus2["TRASH"] = "TRASH";
  DeleteStatus2["DELETED"] = "DELETED";
  DeleteStatus2["PENDING_PURGE"] = "PENDING_PURGE";
  DeleteStatus2["PURGED_EXTERNALLY"] = "PURGED_EXTERNALLY";
})(DeleteStatus || (DeleteStatus = {}));
var WebhookIdentityType;
(function(WebhookIdentityType2) {
  WebhookIdentityType2["UNKNOWN"] = "UNKNOWN";
  WebhookIdentityType2["ANONYMOUS_VISITOR"] = "ANONYMOUS_VISITOR";
  WebhookIdentityType2["MEMBER"] = "MEMBER";
  WebhookIdentityType2["WIX_USER"] = "WIX_USER";
  WebhookIdentityType2["APP"] = "APP";
})(WebhookIdentityType || (WebhookIdentityType = {}));

// ../velo-client/src/schemas.ts
init_esm_shims();
var createAppSchema = external_exports.object({
  app: external_exports.object({ id: external_exports.string() })
});
var GitHubRepositorySchema = external_exports.object({
  id: external_exports.string(),
  repositoryName: external_exports.string(),
  htmlUrl: external_exports.string(),
  cloneUrl: external_exports.string()
}).passthrough();
var gitHubDisabledSchema = external_exports.object({
  onboardingState: external_exports.literal(OnboardingState.GITHUB_DISABLED)
}).passthrough();
var gitHubRepositoryCreatedSchema = external_exports.object({
  onboardingState: external_exports.literal(OnboardingState.GITHUB_REPOSITORY_CREATED),
  gitHubRepositoryCreated: external_exports.object({
    repository: GitHubRepositorySchema
  }).passthrough()
}).passthrough();
var gitHubEnabledSchema = external_exports.object({
  onboardingState: external_exports.literal(OnboardingState.GITHUB_ENABLED),
  gitHubEnabled: external_exports.object({
    repository: GitHubRepositorySchema
  }).passthrough()
}).passthrough();
var gitHubOnboardingStatusSchema = external_exports.discriminatedUnion("onboardingState", [
  gitHubDisabledSchema,
  gitHubRepositoryCreatedSchema,
  gitHubEnabledSchema
]);
var gitHubOnboardingStatusResponseSchema = external_exports.object({
  gitHubOnboardingStatus: gitHubOnboardingStatusSchema
}).passthrough();
var listFilesSchema = external_exports.object({
  files: external_exports.array(
    external_exports.object({
      path: external_exports.string(),
      content: external_exports.string()
    })
  ),
  directories: external_exports.array(
    external_exports.object({
      path: external_exports.string()
    })
  )
});
var deployAppSchema = external_exports.object({
  deploymentInfo: external_exports.object({
    deploymentId: external_exports.string().uuid(),
    deploymentUrl: external_exports.string().url(),
    deploymentShortUrl: external_exports.string().url().optional(),
    isPublishPipelineDeployment: external_exports.boolean()
  })
});
var resolveNpmDependenciesSchema = external_exports.object({
  jobId: external_exports.string()
});
var getResolveNpmDependenciesResultSchema = external_exports.object({
  result: external_exports.object({
    npmDependenciesLockFile: external_exports.string().url()
  })
});
var getWixLockFileSchema = external_exports.string();

// ../velo-client/src/velo-client.ts
var { minRetryTimeout, pollInterval, pollTimeout } = getTestOverrides();
var VeloClient = class {
  constructor(httpClient) {
    this.httpClient = httpClient;
    this.retryOptions = {
      retries: 3,
      minTimeout: minRetryTimeout,
      maxTimeout: 3 * 1e3
    };
  }
  retryOptions;
  createMutableApp = async (content) => {
    try {
      const { data } = await pRetry(
        () => this.httpClient.request(
          createApp({
            app: { mutable: true, appType: AppType.VELO_ISOLATED },
            content
          })
        ),
        this.retryOptions
      );
      return createAppSchema.parse(data).app;
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToCreateVeloApp(),
        cause: e
      });
    }
  };
  listAppFiles = async (appId) => {
    try {
      const { data } = await pRetry(
        () => this.httpClient.request(
          listFiles({
            gridAppId: appId,
            layout: GridAppLayout.GITHUB,
            hideDeletedPages: true,
            includeNewPages: false
          })
        ),
        this.retryOptions
      );
      return listFilesSchema.parse(data);
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToGetFilesFromVelo(),
        cause: e
      });
    }
  };
  updateAppFiles = async (appId, content) => {
    try {
      await pRetry(
        () => this.httpClient.request(
          updateFiles({
            ...content,
            gridAppId: appId
          })
        ),
        this.retryOptions
      );
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToUpdateVeloAppFiles(),
        cause: e
      });
    }
  };
  deploySitePreview = async (deploymentSourceData) => {
    try {
      const { data } = await this.httpClient.request(
        deployApp({
          ...deploymentSourceData,
          deploymentOperation: DeploymentOperation.RC,
          appType: AppType.VELO,
          rcDeploymentParams: {
            rcLabel: RcLabel.WIX_CLI
          }
        })
      );
      return deployAppSchema.parse(data).deploymentInfo;
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToDeploySitePreview(),
        cause: e
      });
    }
  };
  generateWixLockFile = async (deps) => {
    const jobId = await this.resolveNpmDependencies(deps);
    const wixLockFileUrl = await this.getResolveNpmDependenciesResult(jobId);
    return this.getWixLockFile(wixLockFileUrl);
  };
  resolveNpmDependencies = async (npmPackageInfos) => {
    try {
      const { data } = await pRetry(
        () => this.httpClient.request(resolveNpmDependencies({ npmPackageInfos })),
        this.retryOptions
      );
      const { jobId } = resolveNpmDependenciesSchema.parse(data);
      return jobId;
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToResolveNpmDependencies(),
        cause: e
      });
    }
  };
  getResolveNpmDependenciesResult = async (jobId) => {
    try {
      return await pWaitFor(
        async () => {
          try {
            const { data } = await pRetry(
              () => this.httpClient.request(
                getResolveNpmDependenciesResult({ jobId })
              ),
              this.retryOptions
            );
            const { result } = getResolveNpmDependenciesResultSchema.parse(data);
            return pWaitFor.resolveWith(result.npmDependenciesLockFile);
          } catch (e) {
            if (isHttpError(e) && e.response?.status === 428) {
              return false;
            }
            throw e;
          }
        },
        {
          // Poll each 2 seconds
          interval: pollInterval ?? 2 * 1e3,
          // Fail if 5 minutes passed
          timeout: pollTimeout ?? 5 * 60 * 1e3
        }
      );
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToGetResolveNpmDependenciesResult(),
        cause: e,
        info: { jobId }
      });
    }
  };
  getWixLockFile = async (url) => {
    try {
      const standaloneHttpClient = createHttpClient({ type: "standalone" });
      const { data } = await pRetry(
        () => standaloneHttpClient.get(url),
        this.retryOptions
      );
      return getWixLockFileSchema.parse(data);
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToFetchWixLockFile(),
        cause: e
      });
    }
  };
  getGitHubOnboardingStatus = async (projectId) => {
    try {
      const { data } = await this.httpClient.request({
        method: "GET",
        url: `https://manage.wix.com/_api/wix-code-github-onboarding/v1/onboarding-status?projectId=${projectId}`
      }).catch((error) => {
        if (isHttpError(error) && isHttpApplicationError(error)) {
          return { data: null };
        }
        throw error;
      });
      if (!data) {
        return null;
      }
      const parsed = gitHubOnboardingStatusResponseSchema.parse(data);
      const { gitHubOnboardingStatus } = parsed;
      if (gitHubOnboardingStatus.onboardingState === OnboardingState.GITHUB_REPOSITORY_CREATED) {
        const { repository } = gitHubOnboardingStatus.gitHubRepositoryCreated;
        return {
          status: OnboardingState.GITHUB_REPOSITORY_CREATED,
          repoName: repository.repositoryName,
          cloneUrl: repository.cloneUrl,
          htmlUrl: repository.htmlUrl
        };
      }
      if (gitHubOnboardingStatus.onboardingState === OnboardingState.GITHUB_ENABLED) {
        const { repository } = gitHubOnboardingStatus.gitHubEnabled;
        return {
          status: OnboardingState.GITHUB_ENABLED,
          repoName: repository.repositoryName,
          cloneUrl: repository.cloneUrl,
          htmlUrl: repository.htmlUrl
        };
      }
      return null;
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToGetGitHubOnboardingStatus({ projectId }),
        cause: e
      });
    }
  };
  initGitHubRepository = async (projectId) => {
    try {
      await this.httpClient.request({
        method: "POST",
        url: `https://manage.wix.com/_api/wix-code-github-onboarding/v1/github-repositories/init-velo`,
        data: {
          label: Label.WIX_VIBE,
          projectId
        }
      });
    } catch (e) {
      throw new CliError({
        code: CliErrorCode.FailedToInitGitHubRepository({ projectId }),
        cause: e
      });
    }
  };
};

// ../velo-client/src/useVeloClient.ts
function useVeloClient() {
  const httpClient = useHttpClient({ type: "code" });
  const client = (0, import_react.useMemo)(() => new VeloClient(httpClient), [httpClient]);
  return usePanoramaMethodLogger(client);
}

// ../velo-client/src/index.ts
init_esm_shims();

export {
  OnboardingState,
  useVeloClient
};
//# sourceMappingURL=chunk-ZY7YHRZR.js.map