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

export function isAzureDevOpsRemoteUrl(remoteUrl: string): boolean {
  return /(^git@ssh\.dev\.azure\.com:)|(^https?:\/\/([^@]+@)?dev\.azure\.com\/)|(^ssh:\/\/git@ssh\.dev\.azure\.com\/)/.test(
    remoteUrl.trim()
  );
}

export function buildAzureDevOpsSuperblocksSyncPipeline(domain: string = DEFAULT_SUPERBLOCKS_DOMAIN): string {
  assertValidDomain(domain);
  return `trigger:
  branches:
    include:
      - main

pool:
  vmImage: ubuntu-latest

variables:
  SUPERBLOCKS_DOMAIN: '${domain}'

steps:
  - checkout: self
    fetchDepth: 0
    persistCredentials: false

  - task: Bash@3
    displayName: Sync to Superblocks
    env:
      SUPERBLOCKS_TOKEN: $(SUPERBLOCKS_TOKEN)
      SUPERBLOCKS_DOMAIN: $(SUPERBLOCKS_DOMAIN)
    inputs:
      targetType: inline
      script: |
        docker run --rm \\
          -e SUPERBLOCKS_TOKEN \\
          -e SUPERBLOCKS_DOMAIN \\
          -v "$BUILD_SOURCESDIRECTORY:/github/workspace" \\
          -w /github/workspace \\
          superblocksteam/sync-action:v1
`;
}

export function buildAzureDevOpsSuperblocksSyncPipelineFromBaseUrl(superblocksBaseUrl: string): string {
  return buildAzureDevOpsSuperblocksSyncPipeline(getSuperblocksDomainFromBaseUrl(superblocksBaseUrl));
}
