name: Publish NPM Package

on:
    push:
        tags:
            - '*'
    release:
        types:
            - created

permissions:
    id-token: write
    contents: read

jobs:
    publish-npm-package:
        runs-on: ubuntu-latest

        steps:
            - name: Checkout repository
              uses: actions/checkout@v4
              with:
                  ref: ${{ github.event.pull_request.head.sha || github.ref }}

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: '24.x'
                  registry-url: 'https://registry.npmjs.org'

            - name: Install dependencies
              working-directory: ./assets
              run: yarn install --frozen-lockfile

            - name: Determine version
              id: get_version
              run: |
                  if [ "${{ github.event_name }}" = "release" ]; then
                    VERSION="${{ github.event.release.tag_name }}"
                  else
                    VERSION="${GITHUB_REF_NAME}"
                  fi
                  
                  echo "VERSION=$VERSION" >> $GITHUB_OUTPUT

            - name: Set package.json version
              working-directory: ./assets
              run: npm version --no-git-tag-version --allow-same-version ${{ steps.get_version.outputs.VERSION }}

            - name: Build
              working-directory: ./assets
              run: npm run build

            - name: Publish to npm (OIDC)
              working-directory: ./assets
              run: npm publish --provenance --access public
