name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Checkout [main]
        with:
          fetch-depth: 0
      - name: Setup node
        uses: actions/setup-node@v3.5.1
        with:
          node-version: 18.x
      - run: npm install
      - run: npm run format:check
      - run: npm run type-check
      - run: npm run build
      - name: "Check for unstaged changes"
        run: |
          git status --porcelain
          git diff-index --quiet HEAD -- || exit 1

  compat-modern:
    strategy:
      matrix:
        # rc tag should be react 19 release candidate
        react: [18, rc]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Checkout [main]
        with:
          fetch-depth: 0
      - name: Setup node
        uses: actions/setup-node@v3.5.1
        with:
          node-version: 18.x
      - run: npm install
      - run: npm install react@${{matrix.react}} react-dom@${{matrix.react}} --force
      - run: npm run test:ci
        env:
          REACT_VERSION: ${{matrix.react}}
      - run: npm run build

  compat-legacy:
    strategy:
      matrix:
        react: [16, 17]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Checkout [main]
        with:
          fetch-depth: 0
      - name: Setup node
        uses: actions/setup-node@v3.5.1
        with:
          node-version: 18.x
      - run: npm install
      - run: npm install react@${{matrix.react}} react-dom@${{matrix.react}} @testing-library/react-hooks --force
      - run: npm run test:ci -- --config ./vitest.16.17.config.ts
        env:
          REACT_VERSION: ${{matrix.react}}
      - run: npm run build
