import { DEFAULT_SUPERBLOCKS_DOMAIN, assertValidDomain, getSuperblocksDomainFromBaseUrl } from './github-workflow.js';

export function isGitLabRemoteUrl(remoteUrl: string): boolean {
  return /(^git@gitlab\.com:)|(^https?:\/\/gitlab\.com\/)|(^ssh:\/\/git@gitlab\.com\/)/.test(remoteUrl.trim());
}

function buildGitlabSuperblocksSyncWorkflow(domain: string = DEFAULT_SUPERBLOCKS_DOMAIN): string {
  assertValidDomain(domain);
  return `variables:
  SUPERBLOCKS_DOMAIN: '${domain}'

stages:
  - sync

superblocks-sync:
  stage: sync
  image:
    name: superblocksteam/sync-action:v1
    entrypoint: ['']
    pull_policy: always
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  before_script:
    # Checkout the commit, which triggered the pipeline
    - git checkout "$CI_COMMIT_SHA"
    - git checkout -B "$CI_COMMIT_REF_NAME"
    - git branch --set-upstream-to="origin/$CI_COMMIT_REF_NAME"
  script: /entrypoint.sh
`;
}

export function buildGitlabSuperblocksSyncWorkflowFromBaseUrl(superblocksBaseUrl: string): string {
  return buildGitlabSuperblocksSyncWorkflow(getSuperblocksDomainFromBaseUrl(superblocksBaseUrl));
}
