// NOTICE: This file is generated by Rollup. To modify it,
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const validateTypes = require('../../utils/validateTypes.cjs');
const getRuleSelector = require('../../utils/getRuleSelector.cjs');
const hasPrefix = require('../../utils/hasPrefix.cjs');
const isAutoprefixable = require('../../utils/isAutoprefixable.cjs');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule.cjs');
const optionsMatches = require('../../utils/optionsMatches.cjs');
const parseSelector = require('../../utils/parseSelector.cjs');
const report = require('../../utils/report.cjs');
const ruleMessages = require('../../utils/ruleMessages.cjs');
const validateOptions = require('../../utils/validateOptions.cjs');

const ruleName = 'selector-no-vendor-prefix';

const messages = ruleMessages(ruleName, {
	rejected: (selector) => `Unexpected vendor-prefixed selector "${selector}"`,
});

const meta = {
	url: 'https://stylelint.io/user-guide/rules/selector-no-vendor-prefix',
	fixable: true,
};

/** @type {import('stylelint').CoreRules[ruleName]} */
const rule = (primary, secondaryOptions) => {
	return (root, result) => {
		const validOptions = validateOptions(
			result,
			ruleName,
			{ actual: primary },
			{
				actual: secondaryOptions,
				possible: {
					ignoreSelectors: [validateTypes.isString, validateTypes.isRegExp],
				},
				optional: true,
			},
		);

		if (!validOptions) {
			return;
		}

		root.walkRules((ruleNode) => {
			if (!hasPrefix(ruleNode.selector)) return;

			if (!isStandardSyntaxRule(ruleNode)) {
				return;
			}

			const resolvedRoot = parseSelector(getRuleSelector(ruleNode), result, ruleNode);

			if (!resolvedRoot) return;

			resolvedRoot.walkPseudos((pseudoNode) => {
				const { value } = pseudoNode;

				if (!isAutoprefixable.selector(value)) {
					return;
				}

				if (optionsMatches(secondaryOptions, 'ignoreSelectors', value)) {
					return;
				}

				const fix = () => {
					pseudoNode.value = isAutoprefixable.unprefix(value);
					ruleNode.selector = resolvedRoot.toString();
				};

				const index = pseudoNode.sourceIndex;
				const endIndex = index + value.length;

				report({
					result,
					ruleName,
					message: messages.rejected,
					messageArgs: [value],
					node: ruleNode,
					index,
					endIndex,
					fix,
				});
			});
		});
	};
};

rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;

module.exports = rule;
