"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_SUPERBLOCKS_DOMAIN = void 0;
exports.assertValidDomain = assertValidDomain;
exports.isGitHubRemoteUrl = isGitHubRemoteUrl;
exports.buildGithubSuperblocksSyncWorkflow = buildGithubSuperblocksSyncWorkflow;
exports.getSuperblocksDomainFromBaseUrl = getSuperblocksDomainFromBaseUrl;
exports.buildGithubSuperblocksSyncWorkflowFromBaseUrl = buildGithubSuperblocksSyncWorkflowFromBaseUrl;
exports.DEFAULT_SUPERBLOCKS_DOMAIN = 'app.superblocks.com';
function assertValidDomain(domain) {
    if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]*$/.test(domain)) {
        throw new Error(`Invalid domain for pipeline template: ${domain}`);
    }
}
function isGitHubRemoteUrl(remoteUrl) {
    return /(^git@github\.com:)|(^https?:\/\/github\.com\/)|(^ssh:\/\/git@github\.com\/)/.test(remoteUrl.trim());
}
function buildGithubSuperblocksSyncWorkflow(domain = exports.DEFAULT_SUPERBLOCKS_DOMAIN) {
    assertValidDomain(domain);
    return `name: Sync changes to Superblocks

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  superblocks-sync:
    runs-on: ubuntu-latest
    name: Sync to Superblocks
    permissions:
      contents: read
      packages: read
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

      # Optional: needed only if the CLI version is private in GitHub Packages.
      - name: Setup npm credentials
        env:
          NPM_AUTH_TOKEN: \${{ secrets.NPM_AUTH_TOKEN }}
        run: |
          if [ -n "$NPM_AUTH_TOKEN" ]; then
            echo "@superblocksteam:registry=https://npm.pkg.github.com" >> .npmrc
            echo "always-auth=true" >> .npmrc
            echo "//npm.pkg.github.com/:_authToken=$NPM_AUTH_TOKEN" >> .npmrc
          fi

      - name: Sync
        uses: superblocksteam/sync-action@v1
        env:
          NPM_CONFIG_USERCONFIG: \${{ github.workspace }}/.npmrc
        with:
          token: \${{ secrets.SUPERBLOCKS_TOKEN }}
          domain: ${domain}
          path: .
          sha: \${{ github.sha }}
`;
}
function getSuperblocksDomainFromBaseUrl(superblocksBaseUrl) {
    const trimmed = superblocksBaseUrl.trim();
    if (!trimmed) {
        return exports.DEFAULT_SUPERBLOCKS_DOMAIN;
    }
    try {
        return new URL(trimmed).host || exports.DEFAULT_SUPERBLOCKS_DOMAIN;
    }
    catch {
        return exports.DEFAULT_SUPERBLOCKS_DOMAIN;
    }
}
function buildGithubSuperblocksSyncWorkflowFromBaseUrl(superblocksBaseUrl) {
    return buildGithubSuperblocksSyncWorkflow(getSuperblocksDomainFromBaseUrl(superblocksBaseUrl));
}
//# sourceMappingURL=github-workflow.js.map