'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { jsBasicTypes } from '../../../base-repo/constants/basic-types.js';
import { commonLowerCaseWords, collectionMethodNames } from '../../../base-repo/constants/internal-common-strings.js';
import { isArrayType, isTupleType, nodeTypes, isNameMatch, isMatchesNamedMember, isSameBaseMember } from '@flint/infra/analysis';
import { ruleTypes, url } from '@flint/infra/constants/rules-meta';
import * as _0x2de5a4 from 'typescript';
import { runtimeConstructors } from '../../../base-repo/constants/runtime-constants.js';
import { runtimeStaticMethods } from '@flint/infra/constants/runtime-constants';
import { first } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x52a5fb,_0x52c6e7)=>__defProp(_0x52a5fb,'name',{'value':_0x52c6e7,'configurable':!![]});const ruleName='must-not-needlessly-copy-arrays';const ruleId='mustNotNeedlesslyCopyArrays';const constants={'meta':{'type':ruleTypes['problem'],'docs':{'description':ruleName},'messages':{[ruleId]:ruleName+'\x0a###\x20**Must\x20not**\x20needlessly\x20copy\x20arrays.\x0a\x0a####\x20Wrong\x0a\x0afunction\x20foo(bar:\x20object[])\x20{\x0a\x20\x20\x20\x20return\x20[...bar].filter(...);\x0a}\x0a\x0a####\x20Correct\x0a\x0afunction\x20foo(bar:\x20object[])\x20\x20{\x0a\x20\x20\x20\x20return\x20bar.filter(...);\x0a}\x0a\x0a####\x20Why\x0a\x0aCreating\x20a\x20copy\x20of\x20an\x20array\x20without\x20modifying\x20it\x20is\x20unnecessary\x20and\x20can\x20affect\x20performance.'},'schema':[]},'mutatingMethods':new Set([collectionMethodNames['copyWithin'],collectionMethodNames['fill'],collectionMethodNames['pop'],collectionMethodNames['push'],collectionMethodNames['reverse'],collectionMethodNames['shift'],collectionMethodNames['sort'],collectionMethodNames['splice'],collectionMethodNames['unshift']]),'unionMemberParams':{'fieldName':commonLowerCaseWords['types']}},createRule=ESLintUtils['RuleCreator'](_0x17d5c0=>''+url+_0x17d5c0),mustNotNeedlesslyCopyArrays=createRule({'name':ruleName,'meta':constants['meta'],'defaultOptions':[],'create'(_0x2e804f){const _0x9cad0f=ESLintUtils['getParserServices'](_0x2e804f);if(!_0x9cad0f['program'])return {};return {'CallExpression'(_0x1320b1){isUselessCopy(_0x1320b1,_0x9cad0f)&&_0x2e804f['report']({'messageId':ruleId,'node':_0x1320b1});}};}});function hasUnionMemberCollection(_0x55d369){const _0x223dbf=constants['unionMemberParams']['fieldName'];return _0x223dbf in _0x55d369&&Array['isArray'](_0x55d369[_0x223dbf]);}__name(hasUnionMemberCollection,'hasUnionMemberCollection');function isArrayLikeInUnionType(_0x34f352){return (_0x34f352['flags']&_0x2de5a4['TypeFlags']['Union'])!==0x1e8*0x1+-0x115b+0xf73*0x1&&hasUnionMemberCollection(_0x34f352)&&_0x34f352['types']['every'](_0x109e48=>{return isArrayType(_0x109e48)||isTupleType(_0x109e48);});}__name(isArrayLikeInUnionType,'isArrayLikeInUnionType');function isArrayLikeType(_0x3406f3,_0x155ed1){const _0x22ae82=_0x155ed1['program']['getTypeChecker'](),_0x44a2ac=_0x155ed1['esTreeNodeToTSNodeMap']['get'](_0x3406f3),_0x5794f0=_0x22ae82['getTypeAtLocation'](_0x44a2ac);return isArrayType(_0x5794f0)||isArrayLikeInUnionType(_0x5794f0)||isTupleType(_0x5794f0);}__name(isArrayLikeType,'isArrayLikeType');function isSpreadCopy(_0x30fb35,_0x475f08){let _0x278495=![];if(_0x30fb35['type']===nodeTypes['arrayExpression']&&_0x30fb35['elements']['length']===-0x1c*0x88+-0x250e+-0xa63*-0x5){const _0x567907=first(_0x30fb35['elements']);_0x567907&&_0x567907['type']===nodeTypes['spreadElement']&&isArrayLikeType(_0x567907['argument'],_0x475f08)&&(_0x278495=!![]);}return _0x278495;}__name(isSpreadCopy,'isSpreadCopy');function isZeroOrUndefined(_0x3e6ca0){return !!_0x3e6ca0&&(_0x3e6ca0['type']===nodeTypes['literal']&&_0x3e6ca0['value']===0x19fc+0x13b+0x1b37*-0x1||_0x3e6ca0['type']===nodeTypes['identifier']&&isNameMatch(_0x3e6ca0,jsBasicTypes['undefined']));}__name(isZeroOrUndefined,'isZeroOrUndefined');function isUsedLengthMethod(_0x2c3532,_0x114246){return _0x114246['type']===nodeTypes['memberExpression']&&isMatchesNamedMember(_0x114246,void(0x1ef*-0xf+0x3*-0x2fb+0x2*0x12f9),collectionMethodNames['length'])&&isSameBaseMember(_0x2c3532,_0x114246);}__name(isUsedLengthMethod,'isUsedLengthMethod');function isIdentitySlice(_0xe7c99,_0x4d6313,_0x300e09){return isZeroOrUndefined(_0x4d6313)&&(!_0x300e09||_0x300e09['type']===nodeTypes['identifier']&&isNameMatch(_0x300e09,jsBasicTypes['undefined'])||isUsedLengthMethod(_0xe7c99['object'],_0x300e09));}__name(isIdentitySlice,'isIdentitySlice');function isSliceCopy(_0x27d26e,_0x1d9f05){let _0x12b5e5=![];if(_0x27d26e['type']===nodeTypes['callExpression']&&_0x27d26e['callee']['type']===nodeTypes['memberExpression']&&isMatchesNamedMember(_0x27d26e['callee'],void(0x772*-0x4+-0x1*-0x22d9+0x1*-0x511),collectionMethodNames['slice'])&&isArrayLikeType(_0x27d26e['callee']['object'],_0x1d9f05)){const [_0x2c9026,_0x5efe81]=_0x27d26e['arguments'];_0x12b5e5=_0x27d26e['arguments']['length']===0x1311+0x1*0x18a3+-0x2bb4||isIdentitySlice(_0x27d26e['callee'],_0x2c9026,_0x5efe81);}return _0x12b5e5;}__name(isSliceCopy,'isSliceCopy');function isSingleArgumentAnArray(_0x1e8a3b,_0x85306){let _0x5b4e1b=![];if(_0x1e8a3b['length']===0x71d+-0x1*-0x8da+-0xff6){const _0x30b79d=first(_0x1e8a3b),_0x35621f=_0x30b79d['type']===nodeTypes['spreadElement']?_0x30b79d['argument']:_0x30b79d;_0x5b4e1b=isArrayLikeType(_0x35621f,_0x85306);}return _0x5b4e1b;}__name(isSingleArgumentAnArray,'isSingleArgumentAnArray');function isEmptyArrayConcat(_0x284712,_0x3e0c17,_0x1b6c55){return _0x284712['object']['type']===nodeTypes['arrayExpression']&&_0x284712['object']['elements']['length']===0x26c1+0x102c+-0x36ed&&isSingleArgumentAnArray(_0x3e0c17,_0x1b6c55);}__name(isEmptyArrayConcat,'isEmptyArrayConcat');function isArrayLikeConcat(_0x261ecd,_0x1fc569,_0x8ae316){let _0x2c3795=![];if(isArrayLikeType(_0x261ecd['object'],_0x8ae316)){if(_0x1fc569['length']===-0x10b9+0x2226*-0x1+0x32df)_0x2c3795=!![];else {if(_0x1fc569['length']===-0x7*0x3fa+0xeeb+0xcec){const _0x14092f=first(_0x1fc569);_0x14092f['type']===nodeTypes['arrayExpression']&&_0x14092f['elements']['length']===-0x490+-0x1c9c+-0x1096*-0x2&&(_0x2c3795=!![]);}}}return _0x2c3795;}__name(isArrayLikeConcat,'isArrayLikeConcat');function isConcatCopy(_0x350aa7,_0x875af2){return _0x350aa7['type']===nodeTypes['callExpression']&&_0x350aa7['callee']['type']===nodeTypes['memberExpression']&&isMatchesNamedMember(_0x350aa7['callee'],void(-0x1b64+-0x9*0x38e+0x3b62),collectionMethodNames['concat'])&&(isEmptyArrayConcat(_0x350aa7['callee'],_0x350aa7['arguments'],_0x875af2)||isArrayLikeConcat(_0x350aa7['callee'],_0x350aa7['arguments'],_0x875af2));}__name(isConcatCopy,'isConcatCopy');function isArrayFromCopy(_0x52543a,_0x1bdb3d){return _0x52543a['type']===nodeTypes['callExpression']&&_0x52543a['callee']['type']===nodeTypes['memberExpression']&&isMatchesNamedMember(_0x52543a['callee'],runtimeConstructors['array'],runtimeStaticMethods['from'])&&isSingleArgumentAnArray(_0x52543a['arguments'],_0x1bdb3d);}__name(isArrayFromCopy,'isArrayFromCopy');function isNonMutatingMethod(_0x290473){return _0x290473['property']['type']===nodeTypes['identifier']&&!constants['mutatingMethods']['has'](_0x290473['property']['name'])||_0x290473['property']['type']===nodeTypes['literal']&&typeof _0x290473['property']['value']==='string'&&!constants['mutatingMethods']['has'](_0x290473['property']['value']);}__name(isNonMutatingMethod,'isNonMutatingMethod');function isArrayCopySyntax(_0x5e7a92,_0xa32469){return isSpreadCopy(_0x5e7a92,_0xa32469)||isSliceCopy(_0x5e7a92,_0xa32469)||isConcatCopy(_0x5e7a92,_0xa32469)||isArrayFromCopy(_0x5e7a92,_0xa32469);}__name(isArrayCopySyntax,'isArrayCopySyntax');function isUselessCopy(_0x5beef9,_0xd86d4f){return _0x5beef9['callee']['type']===nodeTypes['memberExpression']&&isArrayCopySyntax(_0x5beef9['callee']['object'],_0xd86d4f)&&isNonMutatingMethod(_0x5beef9['callee']);}__name(isUselessCopy,'isUselessCopy');

export { mustNotNeedlesslyCopyArrays as default, ruleId, ruleName };
