name: Build Element Call
on:
  workflow_call:
    inputs:
      vite_app_version:
        required: true
        type: string
      package:
        type: string # This would ideally be a `choice` type, but that isn't supported yet
        description: The package type to be built. Must be one of 'full', 'embedded', or 'sdk'
        required: true
      build_mode:
        type: string # This would ideally be a `choice` type, but that isn't supported yet
        description: The build mode for vite. Must be either 'development' or 'production'
        required: false
        default: production
    secrets:
      SENTRY_ORG:
        required: true
      SENTRY_PROJECT:
        required: true
      SENTRY_URL:
        required: true
      SENTRY_AUTH_TOKEN:
        required: true
      CODECOV_TOKEN:
        required: false

jobs:
  build:
    name: Build Element Call
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false
      - name: Enable Corepack
        run: corepack enable
      - name: pnpm cache
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
        with:
          cache: "pnpm"
          node-version-file: ".node-version"
      - name: Install dependencies
        # ignore-pnpmfile should never be commited. Make CI crash if it happened (`pnpmfileChecksum` is present)
        run: "pnpm install --frozen-lockfile --ignore-pnpmfile"
      - name: Build Element Call
        run: pnpm run build:"$PACKAGE":"$BUILD_MODE"
        env:
          SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
          SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
          SENTRY_URL: ${{ secrets.SENTRY_URL }}
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
          VITE_APP_VERSION: ${{ inputs.vite_app_version }}
          NODE_OPTIONS: "--max-old-space-size=4096"
          PACKAGE: ${{ inputs.package }}
          BUILD_MODE: ${{ inputs.build_mode }}
      - name: Upload Artifact
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: build-output-${{ inputs.package }}
          path: dist
          # We'll only use this in a triggered job, then we're done with it
          retention-days: 1
