name: 🛡️ Validate
on:
  workflow_call:
  pull_request:
  push:
    branches:
      - main

permissions:
  contents: read

jobs:
  checks:
    name: Checks
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v5.0.0

      - name: Setup
        uses: ./.github/actions/setup

      - name: Basic Checks
        run: pnpm check

  builds:
    name: Builds
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v5.0.0

      - name: Setup
        uses: ./.github/actions/setup

      - name: Build All Packages
        run: pnpm buildc all

  build-demo:
    name: Build Demo
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v5.0.0

      - name: Setup
        uses: ./.github/actions/setup

      - name: Build
        run: pnpm build:all
        working-directory: packages/wxt-demo

      - name: ZIP
        run: pnpm wxt zip
        working-directory: packages/wxt-demo

  tests:
    name: Tests (${{ matrix.title }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - title: 'Linux'
            os: ubuntu-22.04
            coverage: true
          - title: 'Windows'
            os: windows-latest
            coverage: false
    steps:
      - name: Checkout
        uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v5.0.0

      - name: Setup
        uses: ./.github/actions/setup

      - name: Setup Bun
        uses: oven-sh/setup-bun@22457c87c1b161cf7dde222c3e82b2b5f8d2bed2 # v2.0.2

      - name: Run Tests
        if: ${{ ! matrix.coverage }}
        run: pnpm test

      - name: Run Tests (Coverage)
        if: matrix.coverage
        run: pnpm test:coverage --reporter=default --reporter=hanging-process

      - name: Upload Coverage
        if: matrix.coverage
        uses: codecov/codecov-action@fdcc8476540edceab3de004e990f80d881c6cc00 # v5.5.0
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  template:
    name: Template
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        template:
          - react
          - solid
          - svelte
          - vanilla
          - vue
    steps:
      - name: Checkout
        uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v5.0.0

      - name: Setup
        uses: ./.github/actions/setup

      - name: Pack WXT package
        run: pnpm pack
        working-directory: packages/wxt

      - name: Install Dependencies
        run: npm i
        working-directory: templates/${{ matrix.template }}

      - name: Install Packed WXT
        run: npm i -D ../../packages/wxt/wxt-*.tgz
        working-directory: templates/${{ matrix.template }}

      - name: Type Check Template
        run: pnpm compile
        if: matrix.template != 'svelte'
        working-directory: templates/${{ matrix.template }}

      - name: Type Check Template
        run: pnpm check
        if: matrix.template == 'svelte'
        working-directory: templates/${{ matrix.template }}

      - name: Build Template
        run: pnpm build
        working-directory: templates/${{ matrix.template }}
