'use strict';
import { nodeTypes } from '@flint/infra/analysis';
import { createEslintRule } from '@flint/infra/create-eslint-rule';
import { mutationActionNames } from '../../../base-repo/constants/internal-common-strings.js';

var __defProp=Object['defineProperty'],__name=(_0x51d8a4,_0x3dbf9d)=>__defProp(_0x51d8a4,'name',{'value':_0x3dbf9d,'configurable':!![]});const ruleName='must-use-wrappers-to-access-first-element';const ruleContent='\x0a###\x20**Must**\x20use\x20wrappers\x20to\x20access\x20first\x20element.\x0a\x0aInstead\x20of\x20indexing\x20with\x20[0],\x20use\x20one\x20of\x20the\x20helper\x20functions:\x0a-\x20singleOrUndefined:\x20when\x20the\x20array\x20is\x20expected\x20to\x20contain\x20no\x20more\x20than\x201\x20element\x20(that\x20is,\x200\x20or\x201).\x20Returns\x20undefined\x20if\x20empty\x20and\x20throws\x20if\x20there\x20are\x20multiple\x20elements;\x0a-\x20singleOrUndefinedWithWarning:\x20when\x20the\x20array\x20is\x20expected\x20to\x20contain\x20no\x20more\x20than\x201\x20element\x20(that\x20is,\x200\x20or\x201),\x20but\x20you\x20still\x20want\x20a\x20warning\x20if\x20the\x20array\x20has\x20more\x20than\x201\x20element;\x0a-\x20singleWithWarning:\x20when\x20the\x20array\x20is\x20expected\x20to\x20contain\x20exactly\x201\x20element.\x20Throws\x20if\x20the\x20array\x20is\x20empty;\x0a-\x20first:\x20when\x20the\x20array\x20is\x20expected\x20to\x20be\x20non-empty.\x20Throws\x20if\x20the\x20array\x20is\x20empty;\x0a-\x20firstOrUndefined:\x20when\x20the\x20array\x20may\x20be\x20empty\x20and\x20you\x20want\x20the\x20first\x20element\x20or\x20undefined.\x0a\x0aUse\x20these\x20wrappers\x20to\x20make\x20array-size\x20expectations\x20explicit\x20and\x20help\x20identify\x20data\x20inconsistencies.\x0a\x0aIf\x20you\x20expect\x20the\x20array\x20to\x20contain\x20exactly\x201\x20element,\x20prefer\x20`singleWithWarning`.\x20If\x200\x20or\x201\x20element\x20is\x20expected,\x20prefer\x20`singleOrUndefined`\x20or\x20`singleOrUndefinedWithWarning`\x20over\x20`first`.\x0a\x0a####\x20Wrong\x0a\x0aasync\x20function\x20findUserById(id:\x20string)\x20{\x0a\x20\x20\x20\x20return\x20(await\x20db.select().from(users).where(eq(users.id,\x20id)))[0];\x0a}\x0a\x0afunction\x20getElementById(id:\x20string)\x20{\x0a\x20\x20\x20\x20return\x20document.querySelectorAll(id)[0];\x0a}\x0a\x0aconst\x20cheapestProduct\x20=\x20products.toSorted((a,\x20b)\x20=>\x20a.price\x20-\x20b.price)[0];\x0a\x0aconst\x20latestErrorLog\x20=\x20logs\x0a\x20\x20\x20\x20.filter((log)\x20=>\x20log.level\x20===\x20\x22error\x22)\x0a\x20\x20\x20\x20.toSorted((a,\x20b)\x20=>\x20b.timestamp\x20-\x20a.timestamp)[0];\x0a\x0a####\x20Correct\x0a\x0aasync\x20function\x20findUserById(id:\x20string)\x20{\x0a\x20\x20\x20\x20return\x20singleOrUndefined(await\x20db.select().from(users).where(eq(users.id,\x20id)));\x0a}\x0a\x0afunction\x20getElementById(id:\x20string)\x20{\x0a\x20\x20\x20\x20return\x20singleOrUndefinedWithWarning(document.querySelectorAll(id));\x0a}\x0a\x0aconst\x20cheapestProduct\x20=\x20first(products.toSorted((a,\x20b)\x20=>\x20a.price\x20-\x20b.price));\x0a\x0aconst\x20latestErrorLog\x20=\x20firstOrUndefined(\x0a\x20\x20\x20\x20logs.filter((log)\x20=>\x20log.level\x20===\x20\x22error\x22).toSorted((a,\x20b)\x20=>\x20b.timestamp\x20-\x20a.timestamp),\x0a);\x0a\x0a####\x20Why\x0a\x0aIndexing\x20with\x20[0]\x20hides\x20array-size\x20assumptions\x20and\x20makes\x20data\x20inconsistencies\x20harder\x20to\x20notice.';var mustUseWrappersToAccessFirstElement = createEslintRule(ruleName,ruleContent,__name(function create(_0x5a69e2){return {'MemberExpression'(_0x43f354){if(!_0x43f354['computed'])return;if(_0x43f354['property']['type']!==nodeTypes['literal']||_0x43f354['property']['value']!==-0x164*-0x1b+0x2386+-0x4912)return;!isInWriteContext(_0x43f354)&&_0x5a69e2['report']({'node':_0x43f354['property'],'messageId':ruleName});}};},'create'));function isInWriteContext(_0x4f4b5c){const _0x48820b=_0x4f4b5c['parent'];if(!_0x48820b)return ![];return _0x48820b['type']===nodeTypes['assignmentExpression']&&_0x48820b['left']===_0x4f4b5c||_0x48820b['type']===nodeTypes['updateExpression']||_0x48820b['type']===nodeTypes['unaryExpression']&&_0x48820b['operator']===mutationActionNames['delete'];}__name(isInWriteContext,'isInWriteContext');

export { mustUseWrappersToAccessFirstElement as default, ruleName };
