import { FlatCompat } from "@eslint/eslintrc"
import js from "@eslint/js"
import typescriptEslint from "@typescript-eslint/eslint-plugin"
import tsParser from "@typescript-eslint/parser"
import { defineConfig } from "eslint/config"
import formatjs from "eslint-plugin-formatjs"
import simpleImportSort from "eslint-plugin-simple-import-sort"

const compat = new FlatCompat({
  recommendedConfig: js.configs.recommended,
  allConfig: js.configs.all,
})

export default defineConfig([
  {
    extends: compat.extends("next/core-web-vitals", "plugin:import/typescript"),
    plugins: {
      "simple-import-sort": simpleImportSort,
      "@typescript-eslint": typescriptEslint,
      formatjs,
    },

    languageOptions: {
      parser: tsParser,
    },

    rules: {
      "no-unused-vars": "off",
      "react/function-component-definition": "error",

      "simple-import-sort/imports": [
        "error",
        {
          groups: [
            ["^\\u0000"],
            ["^node:"],
            ["^@?\\w"],
            ["^@scandic-hotels/(?!.*\u0000$).*$"],
            [
              "^@/constants/?(?!.*\u0000$).*$",
              "^@/env/?(?!.*\u0000$).*$",
              "^@/lib/?(?!.*\u0000$).*$",
              "^@/server/?(?!.*\u0000$).*$",
              "^@/stores/?(?!.*\u0000$).*$",
            ],
            ["^@/(?!(types|.*\u0000$)).*$"],
            [
              "^\\.\\.(?!/?$)",
              "^\\.\\./?$",
              "^\\./(?=.*/)(?!/?$)",
              "^\\.(?!/?$)",
              "^\\./?$",
            ],
            ["^(?!\\u0000).+\\.s?css$"],
            ["^node:.*\\u0000$", "^@?\\w.*\\u0000$"],
            [
              "^@scandichotels/.*\\u0000$",
              "^@/types/.*",
              "^@/.*\\u0000$",
              "^[^.].*\\u0000$",
              "^\\..*\\u0000$",
            ],
          ],
        },
      ],

      "simple-import-sort/exports": "error",
      "import/first": "error",
      "import/newline-after-import": "error",

      "import/no-duplicates": [
        "error",
        {
          "prefer-inline": true,
        },
      ],

      "@typescript-eslint/consistent-type-imports": "error",

      "@typescript-eslint/no-unused-vars": [
        "error",
        {
          args: "all",
          argsIgnorePattern: "^_",
          caughtErrors: "all",
          caughtErrorsIgnorePattern: "^_",
          destructuredArrayIgnorePattern: "^_",
          varsIgnorePattern: "^_",
          ignoreRestSiblings: true,
        },
      ],

      "formatjs/enforce-default-message": ["error", "literal"],
      "formatjs/enforce-placeholders": ["error"],
      "formatjs/enforce-plural-rules": ["error"],
      "formatjs/no-literal-string-in-jsx": ["error"],
      "formatjs/no-multiple-whitespaces": ["error"],
      "formatjs/no-multiple-plurals": ["error"],
      "formatjs/no-invalid-icu": ["error"],
      "formatjs/no-id": ["error"],
      "formatjs/no-complex-selectors": ["error"],
      "formatjs/no-useless-message": ["error"],
      "formatjs/prefer-pound-in-plural": ["error"],
    },
  },
])
