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' or 'embedded'
        required: true
    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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Enable Corepack
        run: corepack enable
      - name: Yarn cache
        uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4
        with:
          cache: "yarn"
          node-version-file: ".node-version"
      - name: Install dependencies
        run: "yarn install --immutable"
      - name: Build full version
        if: ${{ inputs.package == 'full' }}
        run: "yarn run build:full"
        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"
      - name: Build embedded
        if: ${{ inputs.package == 'embedded' }}
        run: "yarn run build:embedded"
        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"
      - 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
