local eslint = import 'defaults/pre-commit-config/eslint.libjsonnet';

/**
 * @file defaults/pre-commit-config/local.libjsonnet
 * @namespace pre_commit_config::local
 * @brief Local hook configuration for `.pre-commit-config.yaml`.
 */
{
  local python_hooks(settings) = [{
    entry: '%s ruff check --fix --exit-non-zero-on-fix' % (
      if settings.package_manager == 'uv' then 'uv run' else 'poetry run'
    ),
    id: 'fix-ruff',
    language: 'system',
    name: 'check Python files have Ruff fixes applied',
    require_serial: true,
    types_or: [
      'python',
      'pyi',
    ],
  }],
  local eslint_hooks = [eslint],
  /**
   * @brief Generate local hook configuration based on settings.
   * @param settings Object containing configuration settings that may influence the generated
   * hooks.
   * @returns An object containing the hooks to be added to the pre-commit configuration.
   */
  get(settings):: {
    hooks: [
      {
        entry: 'yarn install --check-cache --immutable',
        files: '^package\\.json$',
        id: 'yarn-check-lock',
        language: 'system',
        name: 'check yarn.lock is up-to-date',
        pass_filenames: false,
      },
      {
        always_run: true,
        entry: 'yarn install',
        id: 'yarn-install',
        language: 'system',
        name: 'ensure Node packages are installed for this branch',
        pass_filenames: false,
        stages: [
          'post-checkout',
          'post-merge',
        ],
      },
      {
        entry: 'yarn prettier -w',
        exclude: '((requirements|robots).txt|Dockerfile.*|..*ignore|.(coveragerc|gitattributes)|.*.(csv|lock|resource|robot)|CODEOWNERS|py.typed)$',
        exclude_types: [
          'binary',
          'dockerfile',
          'pyi',
          'python',
          'rst',
          'plain-text',
          'shell',
        ],
        id: 'fix-formatting-prettier',
        language: 'system',
        name: 'check files are formatted with Prettier',
      },
    ] + (if settings.project_type == 'python' then python_hooks(settings) else []) + (if settings.force_eslint then eslint_hooks else []) + [
      {
        entry: 'yarn markdownlint-cli2 --config package.json --configPointer /markdownlint-cli2 --fix',
        id: 'fix-formatting-markdown',
        language: 'system',
        name: 'check Markdown files are formatted',
        types_or: [
          'markdown',
        ],
      },
      {
        entry: 'yarn cspell --no-progress --no-must-find-files --no-summary',
        id: 'cspell',
        language: 'node',
        name: 'check spelling',
        types: ['text'],
      },
    ],
    repo: 'local',
  },
}
