name: Publish package to npm

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: 22.14.0
          registry-url: https://registry.npmjs.org
          package-manager-cache: false

      - name: Update npm for trusted publishing
        run: npm install --global npm@11

      - name: Install dependencies
        run: npm install

      - name: Build package
        run: npm run build

      - name: Determine npm dist-tag
        id: npm_dist_tag
        shell: bash
        run: |
          VERSION="${GITHUB_REF_NAME#v}"

          if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$ ]]; then
            echo "Unsupported release tag: ${GITHUB_REF_NAME}" >&2
            exit 1
          fi

          if [[ "$VERSION" == *"-beta."* ]]; then
            echo "value=beta" >> "$GITHUB_OUTPUT"
          else
            echo "value=latest" >> "$GITHUB_OUTPUT"
          fi

      - name: Publish package
        run: npm publish --tag "${{ steps.npm_dist_tag.outputs.value }}"
