name: Get number of threads
description: Determines number of threads to use on macOS and Linux
inputs:
  subtract_threads:
    description: How many threads to subtract from the calculated number
    required: true
    default: "0"
outputs:
  threads_number:
    description: Number of threads to use
    value: ${{ steps.number_of_threads_export.outputs.num }}
runs:
  using: composite
  steps:
    - name: Get number of threads on mac
      id: mac_threads
      if: ${{ runner.os == 'macOS' }}
      shell: bash
      run: echo "num=$(($(sysctl -n hw.logicalcpu) - 2))" >> $GITHUB_OUTPUT

    - name: Get number of threads on Linux
      id: linux_threads
      if: ${{ runner.os == 'Linux' }}
      shell: bash
      run: echo "num=$(($(nproc) - 2))" >> $GITHUB_OUTPUT

    - name: Shift and export number of threads
      id: number_of_threads_export
      shell: bash
      run: |
        num_of_threads=${{ steps.mac_threads.outputs.num || steps.linux_threads.outputs.num }}
        shift_by=${{ inputs.subtract_threads }}
        shifted=$((num_of_threads - shift_by))
        echo "num=$(( shifted > 1 ? shifted : 1 ))" >> $GITHUB_OUTPUT
