content: -- editorconfig-checker-disable-file
  {-# LANGUAGE ApplicativeDo     #-}
  {-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE QuasiQuotes       #-}
  {-# LANGUAGE RecordWildCards   #-}
  
  module Main (main) where
  
  import PlutusTx.Options qualified as O
  
  import Control.Lens
  import Data.Map qualified as Map
  import Data.Text (Text)
  import Data.Text qualified as Text
  import Data.Text.IO qualified as Text
  import Options.Applicative qualified as OA
  import Prettyprinter
  import PyF (fmt)
  
  newtype Params = Params
    {paramOutputFile :: Text}
  
  parseParams :: OA.Parser Params
  parseParams = do
    paramOutputFile <-
      OA.argument OA.str $
        mconcat
          [ OA.metavar "OUTPUT_FILE"
          , OA.help "Output file path"
          ]
    pure Params{..}
  
  main :: IO ()
  main = do
    params <-
      OA.execParser $
        OA.info
          (parseParams OA.<**> OA.helper)
          (OA.fullDesc <> OA.header "Generate plugin option documentation")
    Text.writeFile (Text.unpack $ paramOutputFile params) optionsTable
  
  optionsTable :: Text
  optionsTable =
    Text.stripStart $
      [fmt|
  ---
  sidebar_position: 5
  ---
  
  # Plinth Compiler Options
  
  These options can be passed to the compiler via the `OPTIONS_GHC` pragma, for instance
  
  <!---
  This file is generated by running plutus-tx-plugin:gen-plugin-opts-doc.
  Do NOT modify by hand.
  --->
  
  ``` haskell
  {{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:dump-uplc #-}}
  {{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:max-simplifier-iterations-uplc=3 #-}}
  ```
  
  For each boolean option, you can add a `no-` prefix to switch it off, such as `no-typecheck`, `no-simplifier-beta`.
  
  |Option|Value Type|Default|Description|
  |-|-|-|-|
  {Text.unlines $ uncurry genRow <$> Map.toList O.pluginOptions}
  |]
  
  genRow :: O.OptionKey -> O.PluginOption -> Text
  genRow k (O.PluginOption tr _ field desc _) =
    [fmt||`{k}`|{show tr}|{show (pretty defaultValue)}|{desc}||]
   where
    defaultValue = O.defaultPluginOptions ^. field
  