import wordpress from '@wordpress/eslint-plugin';
import { importX } from 'eslint-plugin-import-x';
import globals from 'globals';

export default [
	...wordpress.configs.recommended,

	// import-x: drop-in replacement for eslint-plugin-import with better TS support
	importX.flatConfigs.recommended,
	importX.flatConfigs.typescript,

	// Project-specific globals and rules
	{
		languageOptions: {
			globals: {
				...globals.browser,
				...globals.node,
				wp: 'readonly',
				warranty_cart: 'readonly',
				warranty_stripe_params: 'readonly',
				ajaxurl: 'readonly',
				Stripe: 'readonly',
				jQuery: 'readonly',
				$: 'readonly',
			},
		},
		rules: {
			'no-console': 'warn',
			'no-debugger': 'error',
			'prefer-const': 'error',
			'no-var': 'error',
			'no-undef': 'off',
			'object-shorthand': 'error',
			'prefer-arrow-callback': 'error',
			'prefer-template': 'error',
			'prettier/prettier': 'off',

			// Disable legacy import plugin order rule, use import-x instead
			'import/order': 'off',
			'import-x/order': [
				'error',
				{
					groups: [ 'external', 'builtin', 'parent', 'sibling', 'index' ],
					'newlines-between': 'always',
					pathGroups: [
						{
							pattern: '@wordpress/**',
							group: 'builtin',
						},
						{
							pattern: 'lucide-react',
							group: 'external',
						},
						{
							pattern: 'framer-motion',
							group: 'external',
							position: 'before',
						},
						{
							pattern: '@/components/**',
							group: 'parent',
						},
						{
							pattern: '@/**',
							group: 'parent',
							position: 'after',
						},
					],
					pathGroupsExcludedImportTypes: [ 'builtin' ],
					alphabetize: {
						order: 'asc',
						caseInsensitive: true,
					},
				},
			],

			'@wordpress/no-unused-vars-before-return': 'error',
			'@wordpress/valid-sprintf': 'error',
			'@wordpress/i18n-text-domain': [
				'error',
				{ allowedTextDomain: 'warranty-cart' },
			],
			'@wordpress/i18n-translator-comments': 'error',
			'@wordpress/i18n-no-variables': 'error',
			'@wordpress/i18n-no-placeholders-only': 'error',
			'@wordpress/i18n-ellipsis': 'error',
			'@wordpress/no-unsafe-wp-apis': 'off',
		},
	},

	// TypeScript-specific overrides
	{
		files: [ '**/*.ts', '**/*.tsx' ],
		rules: {
			'@typescript-eslint/no-unused-vars': 'off',
			'no-unused-vars': 'off',
		},
	},

	// Ignore build artifacts
	{
		ignores: [ 'node_modules/**', 'build/**', 'vendor/**', 'tests/**' ],
	},
];
