name: PUBLISH
on:    
  repository_dispatch:
    types: [publish-command]
  workflow_dispatch:
jobs:
  release-pull-request:
    name: ReleaseAction
    runs-on: ubuntu-latest
    permissions:
      packages: write
    timeout-minutes: 10
    steps:
    - uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        ref: ${{ github.event.client_payload.pull_request.head.ref || github.ref }}
    - uses: actions/setup-node@v4
      with:
        node-version: 20
        registry-url: https://npm.pkg.github.com/
    - name: Install
      run: yarn install --frozen-lockfile
    - name: Get Latest @impargo/react-here-maps version
      id: package-lts-version
      run: | 
        PACKAGE_LTS_VERSION=$(npm view @impargo/react-here-maps dist-tags.latest)
        major_version=$(echo $PACKAGE_LTS_VERSION | cut -d'-' -f1)
        echo "::set-output name=LTS_VERSION::$major_version"
      env:
        NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: publish-gpr
      run:  |
        git config --global user.email "action@github.com"
        git config --global user.name  "GitHub Action"
        yarn publish --no-git-tag-version --new-version ${{ steps.package-lts-version.outputs.LTS_VERSION }}-pre-${{ github.run_number }}
      env:
        NODE_AUTH_TOKEN: ${{ secrets.GHP_TOKEN }}

    - name: Display Version
      run: |
        echo ""
        echo -e "\033[1;34m==========================================\033[0m"
        echo -e "\033[1;32m       🚀 Version: ${{ steps.package-lts-version.outputs.LTS_VERSION }}-pre-${{ github.run_number }}       \033[0m"
        echo -e "\033[1;34m==========================================\033[0m"
        
    - name: Create comment
      uses: peter-evans/create-or-update-comment@v1
      if: github.event_name == 'repository_dispatch'
      with:
        token: ${{ secrets.GHP_TOKEN }}
        repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
        issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
        reactions: rocket,hooray
        body: |
          RUN URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
          PACKAGE VERSION: ${{ steps.package-lts-version.outputs.LTS_VERSION }}-pre-${{ github.run_number }}