'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { createNodeVisitor, isFunction, nodeTypes, getFunctionName } from '@flint/infra/analysis';
import { jsBasicTypes } from '../../../base-repo/constants/basic-types.js';
import { collectionMethodNames, commonLowerCaseWords, contentFormatNames } from '../../../base-repo/constants/internal-common-strings.js';
import { runtimeConstructors } from '../../../base-repo/constants/runtime-constants.js';
import { ruleRecommendationLevels, ruleTypes, url, eslintProperties } from '@flint/infra/constants/rules-meta';
import _0x132a45 from 'pluralize';
import _0x2de5a4__default from 'typescript';
import { runtimeStaticMethods, arrayTypeSyntax } from '@flint/infra/constants/runtime-constants';
import { flintRuleIdentifiers } from '@flint/infra/constants/shared-string-identifiers';
import { firstOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x53fba5,_0x4f0f94)=>__defProp(_0x53fba5,'name',{'value':_0x4f0f94,'configurable':!![]});const ruleName='must-use-plural-in-function-names-if-returns-array';const messageId='usePluralInFunctionName',constants={'meta':{'type':ruleTypes['problem'],'docs':{'description':'Must\x20use\x20plural\x20in\x20function\x20names\x20if\x20the\x20function\x20returns\x20an\x20array\x20and\x20has\x20a\x20fetch-like\x20word\x20in\x20its\x20name.','recommended':ruleRecommendationLevels['error']},'messages':{[messageId]:ruleName+'\x0aFunction\x20\x22{{functionName}}\x22\x20should\x20use\x20plural\x20form\x20\x22{{suggestedName}}\x22\x20since\x20it\x20returns\x20an\x20array\x20and\x20has\x20a\x20fetch-like\x20word\x20in\x20its\x20name.\x0a\x0a###\x20**Must**\x20use\x20plural\x20in\x20function\x20names\x20if\x20the\x20function\x20returns\x20an\x20array\x20and\x20has\x20a\x20fetch-like\x20word\x20(get,\x20fetch,\x20find,\x20etc.)\x20in\x20its\x20name.\x0a\x0a####\x20Wrong\x0a\x0afunction\x20**getItem**(...)\x20{\x0a\x20\x20\x20\x20…\x0a\x20\x20\x20\x20return\x20**[...]**;\x20//\x20array\x0a}\x0a\x0a####\x20Correct\x0a\x0afunction\x20**getItems**(...)\x20{\x0a\x20\x20\x20\x20…\x0a\x20\x20\x20\x20return\x20**[...]**;\x20//\x20array\x0a}\x0a\x0a####\x20Why\x0a\x0aThis\x20improves\x20code\x20readability\x20and\x20helps\x20to\x20understand\x20the\x20purpose\x20of\x20the\x20function.'},'schema':[{'type':jsBasicTypes['object'],'properties':{'exceptions':{'type':jsBasicTypes['array'],'items':{'type':'string'},'description':'List\x20of\x20function\x20names\x20to\x20exclude\x20from\x20plural\x20checking'}},'additionalProperties':![]}]},'identifiers':{'promise':runtimeConstructors['promise'],'array':runtimeConstructors['array']},'collectionWordExceptions':new Set(['act',jsBasicTypes['array'],commonLowerCaseWords['list'],'collection',contentFormatNames['markdown'],'case']),'fetchLikeWords':new Set([commonLowerCaseWords['get'],flintRuleIdentifiers['fetch'],'retrieve',flintRuleIdentifiers['find'],commonLowerCaseWords['list'],flintRuleIdentifiers['load'],commonLowerCaseWords['pull'],flintRuleIdentifiers['search'],'collect']),'definiteArrayMethods':new Set([collectionMethodNames['map'],commonLowerCaseWords['filter'],collectionMethodNames['splice'],collectionMethodNames['sort'],collectionMethodNames['reverse'],collectionMethodNames['flat'],collectionMethodNames['flatMap'],collectionMethodNames['toReversed'],collectionMethodNames['toSorted']])},createRule=ESLintUtils['RuleCreator'](_0x53c1dc=>''+url+_0x53c1dc),mustUsePluralInFunctionNamesIfReturnsArray=createRule({'name':ruleName,'meta':constants['meta'],'defaultOptions':[{}],'create'(_0x5dff15){const _0x4decbb=firstOrUndefined(_0x5dff15['options'])??{},_0x99b311=_0x4decbb['exceptions']??[];return {...createNodeVisitor(_0x428dd2=>checkFunction(_0x428dd2,_0x5dff15,_0x99b311))};}});function isNestedFunction(_0x2d6b4c,_0x131d23){return _0x131d23&&isFunction(_0x2d6b4c);}__name(isNestedFunction,'isNestedFunction');function handleReturnStatement(_0x5c801b,_0x53ebb0){if(_0x5c801b['type']===nodeTypes['returnStatement']&&_0x5c801b['argument'])return analyzeExpression(_0x5c801b['argument'],_0x53ebb0),!![];return ![];}__name(handleReturnStatement,'handleReturnStatement');function traverse(_0x552d8c,_0x50baff,_0x1e3bcd=![]){if(!_0x552d8c||typeof _0x552d8c!==jsBasicTypes['object'])return;if(isNestedFunction(_0x552d8c,_0x1e3bcd))return;if(handleReturnStatement(_0x552d8c,_0x50baff))return;if(_0x552d8c['type']===nodeTypes['blockStatement'])traverseBlockStatement(_0x552d8c['body'],_0x50baff);else {if(_0x552d8c['type']===nodeTypes['ifStatement'])traverseIfStatement(_0x552d8c,_0x50baff);else {if(_0x552d8c['type']===nodeTypes['switchStatement'])traverseSwitchStatement(_0x552d8c['cases'],_0x50baff);else _0x552d8c['type']===nodeTypes['tryStatement']&&traverseTryStatement(_0x552d8c,_0x50baff);}}}__name(traverse,'traverse');function traverseBlockStatement(_0x3c3e3d,_0x4b8f88){for(const _0x3992fb of _0x3c3e3d){_0x3992fb&&traverse(_0x3992fb,_0x4b8f88,!![]);}}__name(traverseBlockStatement,'traverseBlockStatement');function traverseIfStatement(_0x4f0a57,_0xbe0cf1){if(_0x4f0a57['consequent'])traverse(_0x4f0a57['consequent'],_0xbe0cf1,!![]);if(_0x4f0a57['alternate'])traverse(_0x4f0a57['alternate'],_0xbe0cf1,!![]);}__name(traverseIfStatement,'traverseIfStatement');function traverseSwitchStatement(_0x232adc,_0x4b82b1){for(const _0x4031be of _0x232adc){for(const _0xd190e9 of _0x4031be['consequent']){traverse(_0xd190e9,_0x4b82b1,!![]);}}}__name(traverseSwitchStatement,'traverseSwitchStatement');function traverseTryStatement(_0x11a9ef,_0x474265){traverse(_0x11a9ef['block'],_0x474265,!![]);if(_0x11a9ef['handler'])traverse(_0x11a9ef['handler']['body'],_0x474265,!![]);if(_0x11a9ef['finalizer'])traverse(_0x11a9ef['finalizer'],_0x474265,!![]);}__name(traverseTryStatement,'traverseTryStatement');function analyzeReturnStatements(_0x2b3d6e){const _0x35c5aa={'hasExplicitArrays':![],'hasAmbiguousReturns':![],'hasPromiseReturns':![]};if(_0x2b3d6e['type']===nodeTypes['arrowFunctionExpression']&&_0x2b3d6e['body']['type']!==nodeTypes['blockStatement'])return analyzeExpression(_0x2b3d6e['body'],_0x35c5aa),_0x35c5aa;return _0x2b3d6e['body']&&traverse(_0x2b3d6e['body'],_0x35c5aa),_0x35c5aa;}__name(analyzeReturnStatements,'analyzeReturnStatements');function analyzeExpression(_0x3a8c49,_0x99baa0){const _0x566ff7=_0x3a8c49['type']===nodeTypes['arrayExpression']&&_0x3a8c49['elements']?.['some'](_0x4b107b=>_0x4b107b&&_0x4b107b['type']===nodeTypes['spreadElement']);if(_0x3a8c49['type']===nodeTypes['arrayExpression']||isPromiseWithArray(_0x3a8c49)||isDefiniteArrayMethod(_0x3a8c49)||isArrayConstructor(_0x3a8c49)||_0x566ff7){_0x99baa0['hasExplicitArrays']=!![];return;}(_0x3a8c49['type']===nodeTypes['identifier']||_0x3a8c49['type']===nodeTypes['callExpression']||_0x3a8c49['type']===nodeTypes['memberExpression']||_0x3a8c49['type']===nodeTypes['conditionalExpression']||_0x3a8c49['type']===nodeTypes['logicalExpression']||isPromiseReturn(_0x3a8c49))&&(_0x99baa0['hasAmbiguousReturns']=!![]);}__name(analyzeExpression,'analyzeExpression');function isDefiniteArrayMethod(_0x217384){if(_0x217384['type']!==nodeTypes['callExpression'])return ![];if(!_0x217384['callee']||_0x217384['callee']['type']!==nodeTypes['memberExpression'])return ![];if(!_0x217384['callee']['property']||_0x217384['callee']['property']['type']!==nodeTypes['identifier'])return ![];return constants['definiteArrayMethods']['has'](_0x217384['callee']['property']['name']);}__name(isDefiniteArrayMethod,'isDefiniteArrayMethod');function isPromiseWithArray(_0x26cb0c){let _0x5a572a=![];if(_0x26cb0c['type']===nodeTypes['callExpression']&&_0x26cb0c['callee']?.['type']===nodeTypes['memberExpression']&&_0x26cb0c['callee']['object']?.['type']===nodeTypes['identifier']&&_0x26cb0c['callee']['object']['name']===constants['identifiers']['promise']&&_0x26cb0c['callee']['property']?.['type']===nodeTypes['identifier']&&(_0x26cb0c['callee']['property']['name']===runtimeStaticMethods['resolve']||_0x26cb0c['callee']['property']['name']===runtimeStaticMethods['reject'])){const _0x5fbee2=firstOrUndefined(_0x26cb0c['arguments']);_0x5fbee2&&_0x5fbee2['type']===nodeTypes['arrayExpression']&&(_0x5a572a=!![]);}return _0x5a572a;}__name(isPromiseWithArray,'isPromiseWithArray');function isArrayConstructor(_0x44426a){if(_0x44426a['type']===nodeTypes['newExpression']&&_0x44426a['callee']&&_0x44426a['callee']['type']===nodeTypes['identifier']&&_0x44426a['callee']['name']===constants['identifiers']['array'])return !![];return _0x44426a['type']===nodeTypes['callExpression']&&_0x44426a['callee']&&_0x44426a['callee']['type']===nodeTypes['memberExpression']&&_0x44426a['callee']['object']&&_0x44426a['callee']['object']['type']===nodeTypes['identifier']&&_0x44426a['callee']['object']['name']===constants['identifiers']['array']&&_0x44426a['callee']['property']&&_0x44426a['callee']['property']['type']===nodeTypes['identifier']&&(_0x44426a['callee']['property']['name']===runtimeStaticMethods['from']||_0x44426a['callee']['property']['name']===runtimeStaticMethods['of']);}__name(isArrayConstructor,'isArrayConstructor');function isPromiseReturn(_0x4c3871){if(_0x4c3871['type']===nodeTypes['newExpression']&&_0x4c3871['callee']&&_0x4c3871['callee']['type']===nodeTypes['identifier']&&_0x4c3871['callee']['name']===constants['identifiers']['promise'])return !![];return _0x4c3871['type']===nodeTypes['callExpression']&&_0x4c3871['callee']&&_0x4c3871['callee']['type']===nodeTypes['memberExpression']&&_0x4c3871['callee']['object']&&_0x4c3871['callee']['object']['type']===nodeTypes['identifier']&&_0x4c3871['callee']['object']['name']===constants['identifiers']['promise'];}__name(isPromiseReturn,'isPromiseReturn');function isTypeReference(_0x437373){return flintRuleIdentifiers['typeArguments']in _0x437373||(_0x437373['flags']&_0x2de5a4__default['TypeFlags']['Object'])!==-0x18ca+0x3a1*0xa+0x2*-0x5c0;}__name(isTypeReference,'isTypeReference');function unwrapPromiseType(_0x5e7ef2,_0x61a926){if(_0x5e7ef2['symbol']&&_0x5e7ef2['symbol']['name']===constants['identifiers']['promise']&&isTypeReference(_0x5e7ef2)){const _0x4349d1=_0x61a926['getTypeArguments'](_0x5e7ef2),_0x465bef=firstOrUndefined([..._0x4349d1]);if(_0x4349d1&&_0x4349d1['length']>-0x1d*0x3+0x5*-0x7be+0x270d&&_0x465bef)return _0x465bef;}return _0x5e7ef2;}__name(unwrapPromiseType,'unwrapPromiseType');function isArrayReturnType(_0x2d0a7d,_0x57b4cf){let _0x451384=![];if(_0x57b4cf['isArrayType']?.(_0x2d0a7d)||_0x57b4cf['isTupleType']?.(_0x2d0a7d))_0x451384=!![];else {if(_0x2d0a7d['isUnion']?.())_0x451384=_0x2d0a7d['types']['some'](_0x546a0d=>isArrayReturnType(_0x546a0d,_0x57b4cf));else {if(_0x2d0a7d['isIntersection']?.())_0x451384=_0x2d0a7d['types']['every'](_0x1a8b3a=>isArrayReturnType(_0x1a8b3a,_0x57b4cf));else {const _0x12d2f9=_0x57b4cf['typeToString'](_0x2d0a7d);_0x451384=_0x12d2f9['endsWith']('[]')||_0x12d2f9['startsWith'](arrayTypeSyntax['generic']);}}}return _0x451384;}__name(isArrayReturnType,'isArrayReturnType');function analyzeTypeSignature(_0x19f831,_0x1ec9d9){try{const _0xc3cbeb=ESLintUtils['getParserServices'](_0x1ec9d9),_0x2958e9=_0xc3cbeb['program']['getTypeChecker'](),_0x47f970=_0xc3cbeb['esTreeNodeToTSNodeMap']['get'](_0x19f831);if(!_0x47f970)return ![];const _0x1143b7=_0x2958e9['getSignatureFromDeclaration'](_0x47f970);if(!_0x1143b7)return ![];const _0x487e71=_0x2958e9['getReturnTypeOfSignature'](_0x1143b7),_0x3dec65=unwrapPromiseType(_0x487e71,_0x2958e9);return isArrayReturnType(_0x3dec65,_0x2958e9);}catch{return ![];}}__name(analyzeTypeSignature,'analyzeTypeSignature');function returnsArrayByType(_0x2fdb07,_0x1b405c){const _0x351380=analyzeReturnStatements(_0x2fdb07);if(_0x351380['hasExplicitArrays'])return !![];if(_0x351380['hasAmbiguousReturns'])return analyzeTypeSignature(_0x2fdb07,_0x1b405c);return ![];}__name(returnsArrayByType,'returnsArrayByType');function shouldUsePlural(_0x2f7ddb,_0x4af222=[]){if(_0x4af222['includes'](_0x2f7ddb))return ![];const _0x2e9a36=_0x2f7ddb['split'](/(?=[A-Z])/),_0x9cddad=_0x2e9a36['some'](_0x30bca6=>constants['fetchLikeWords']['has'](_0x30bca6['toLowerCase']()));if(!_0x9cddad)return ![];for(const _0x2498e8 of _0x2e9a36){if(_0x132a45['isPlural'](_0x2498e8)||constants['collectionWordExceptions']['has'](_0x2498e8['toLowerCase']()))return ![];}return !![];}__name(shouldUsePlural,'shouldUsePlural');function checkFunction(_0x574064,_0x178bba,_0x458902){const _0xf115df=getFunctionName(_0x574064);if(!_0xf115df)return;const _0x3dd889=_0x178bba['physicalFilename'];if(_0x3dd889&&/\.handlers\.[^.]+$/['test'](_0x3dd889))return;if(_0x574064['parent']['type']===nodeTypes['property']&&_0x574064['parent']['key']['type']===nodeTypes['identifier']&&_0x574064['parent']['key']['name']===eslintProperties['fix'])return;if(returnsArrayByType(_0x574064,_0x178bba)&&shouldUsePlural(_0xf115df,_0x458902)){const _0x1dfdf7=_0x132a45['plural'](_0xf115df);_0x178bba['report']({'node':_0x574064['type']===nodeTypes['functionDeclaration']&&_0x574064['id']?_0x574064['id']:_0x574064,'messageId':messageId,'data':{'functionName':_0xf115df,'suggestedName':_0x1dfdf7}});}}__name(checkFunction,'checkFunction');

export { mustUsePluralInFunctionNamesIfReturnsArray as default, ruleName };
