name: "Restore any Typst package cache"
description: "Configures the caching for Typst packages"
outputs:
  cache-hit:
    description: "Was a cache found with primary key ?"
    value: ${{ steps.cache-typst-restore.outputs.cache-hit }}
  cache-primary-key:
    description: "Key of the cache to find and save"
    value: ${{ steps.cache-typst-restore.outputs.cache-primary-key }}
  cache-matched-key:
    description: "Key of the cache found and used."
    value: ${{ steps.cache-typst-restore.outputs.cache-matched-key }}
  cache-path:
    description: "where is the packages cache for typst ?"
    value: ${{ steps.cache-typst-path.outputs.TYPST_CACHE }}

runs:
  using: "composite"
  steps:
    - name: Typst Cache path
      id: cache-typst-path
      run: |
        case $RUNNER_OS in
          "Linux")
              typst_cache="${XDG_CACHE_HOME:-~/.cache}/typst/packages/"
              ;;
          "macOS")
              typst_cache="~/Library/Caches/typst/packages/"
              ;;
          "Windows")
              typst_cache="$LOCALAPPDATA/typst/packages/"
              ;;
            *)
              echo "$RUNNER_OS not supported"
              exit 1
              ;;
        esac
        echo "TYPST_CACHE=${typst_cache}" >> $GITHUB_ENV
        echo "TYPST_CACHE=${typst_cache}" >> $GITHUB_OUTPUT
      shell: bash

    - name: Cache Typst package folder
      id: cache-typst-restore
      uses: actions/cache/restore@v5
      with:
        path: ${{ env.TYPST_CACHE }}
        key: ${{ runner.os }}-typst-1
        restore-keys: |
          ${{ runner.os }}-typst-
