'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { createNodeVisitor, nodeTypes, getMethodNameFromCallee } from '@flint/infra/analysis';
import { ruleTypes, url } from '@flint/infra/constants/rules-meta';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { externalPackageNames } from '../../../base-repo/app-constants/command-names.js';
import { fileSuffixes } from '../../../base-repo/constants/internal-extensions.js';
import { runtimeConstructors } from '../../../base-repo/constants/runtime-constants.js';
import { runtimeStaticMethods } from '@flint/infra/constants/runtime-constants';
import { flintRuleIdentifiers } from '@flint/infra/constants/shared-string-identifiers';
import { ensurePathSeparatorsOnBothSides } from '../../../fd-toolbox/paths/ensure.js';

var __defProp=Object['defineProperty'],__name=(_0x363974,_0x5d447f)=>__defProp(_0x363974,'name',{'value':_0x5d447f,'configurable':!![]});const ruleName='no-delays-in-ui-tests';const constants={'messageIds':{'noDelaysInTests':'noDelaysInTests'},'meta':{'type':ruleTypes['problem'],'docs':{'description':'Disallow\x20problematic\x20delays\x20and\x20wait\x20methods\x20in\x20Playwright\x20UI\x20tests.\x20Allows\x20valid\x20wait\x20methods\x20like\x20waitForResponse,\x20waitForLoadState,\x20but\x20disallows\x20waitForTimeout,\x20setTimeout,\x20and\x20waitForSelector.'},'messages':{'noDelaysInTests':'no-delays-in-ui-tests\x0a###\x20**Must\x20not**\x20use\x20delays\x20in\x20ui\x20tests.\x0a\x0aAvoid\x20using\x20problematic\x20delays\x20(setTimeout,\x20waitForTimeout,\x20waitForSelector)\x20in\x20UI\x20tests.\x0a\x0a####\x20Wrong\x0a\x0aawait\x20editPo.clickSaveButton(resourceNames.foo,\x202000);\x0a\x0a####\x20Correct\x0a\x0aawait\x20editPo.clickSaveButton(resourceNames.foo);\x20//\x20clickSaveButton\x20should\x20contain\x20logic\x20to\x20wait\x20for\x20element\x0a\x0a####\x20Why\x0a\x0aUsing\x20dynamic\x20waits\x20is\x20better\x20because\x20it\x20ensures\x20stability\x20by\x20adapting\x20to\x20actual\x20conditions,\x20making\x20tests\x20more\x20reliable\x20and\x20faster,as\x20they\x20wait\x20only\x20for\x20necessary\x20actions\x20to\x20complete\x20rather\x20than\x20relying\x20on\x20arbitrary\x20delays.'},'schema':[]},'functionNames':{'setTimeout':'setTimeout','setDefaultTimeout':flintRuleIdentifiers['setDefaultTimeout'],'page':flintRuleIdentifiers['page'],'promise':runtimeConstructors['promise']},'playwrightMethods':[flintRuleIdentifiers['waitForSelector'],flintRuleIdentifiers['setDefaultTimeout'],'waitForTimeout'],'testPatterns':{'testFileExtensions':['.'+folders['test']+'.',fileSuffixes['dotSpecDot']],'testDirectories':[ensurePathSeparatorsOnBothSides(folders['tests']),ensurePathSeparatorsOnBothSides(folders['e2e']),ensurePathSeparatorsOnBothSides(folders['__tests__']),ensurePathSeparatorsOnBothSides(folders['__test__']),ensurePathSeparatorsOnBothSides(folders['test'])]},'imports':{'playwright':externalPackageNames['playwright'],'playwrightTest':externalPackageNames['playwrightTest']},'delayFunctionPatterns':['sleep','delay','pause'],'waitPrefix':'wait'},createRule=ESLintUtils['RuleCreator'](_0x561a9b=>''+url+_0x561a9b),noDelaysInUiTests=createRule({'name':ruleName,'meta':constants['meta'],'defaultOptions':[],'create'(_0x1e191e){let _0x137f6c=![];const _0x3b3e0a=new Set();return {'Program'(){_0x137f6c=checkIsPlaywrightFile(_0x1e191e);},...createNodeVisitor(_0x6f9409=>checkFunctionNode(_0x6f9409,_0x1e191e,_0x137f6c,_0x3b3e0a),[nodeTypes['callExpression'],nodeTypes['newExpression'],nodeTypes['functionDeclaration'],nodeTypes['arrowFunctionExpression'],nodeTypes['functionExpression'],nodeTypes['variableDeclarator']])};}});function isPlaywrightWaitMethod(_0x172895){return constants['playwrightMethods']['some'](_0x1336ff=>_0x1336ff===_0x172895);}__name(isPlaywrightWaitMethod,'isPlaywrightWaitMethod');function isTestFile(_0x1e9f4a){const _0x5d363f=_0x1e9f4a['replace'](/\\/g,'/');return constants['testPatterns']['testFileExtensions']['some'](_0x49fd05=>_0x5d363f['includes'](_0x49fd05))||constants['testPatterns']['testDirectories']['some'](_0x31ac05=>_0x5d363f['includes'](_0x31ac05));}__name(isTestFile,'isTestFile');function isPlaywrightImportOrUsage(_0x4903e3){return _0x4903e3['includes'](constants['imports']['playwright'])||_0x4903e3['includes'](constants['imports']['playwrightTest'])||_0x4903e3['includes']('test(')||_0x4903e3['includes']('test.')||_0x4903e3['includes']('page.')||_0x4903e3['includes']('browser.');}__name(isPlaywrightImportOrUsage,'isPlaywrightImportOrUsage');function isDirectSetTimeoutCall(_0x4e7045){return _0x4e7045['callee']['type']===nodeTypes['identifier']&&_0x4e7045['callee']['name']===constants['functionNames']['setTimeout'];}__name(isDirectSetTimeoutCall,'isDirectSetTimeoutCall');function isPlaywrightWaitCall(_0x396da7){return _0x396da7['callee']['type']===nodeTypes['memberExpression']&&_0x396da7['callee']['property']['type']===nodeTypes['identifier']&&isPlaywrightWaitMethod(_0x396da7['callee']['property']['name']);}__name(isPlaywrightWaitCall,'isPlaywrightWaitCall');function isBrowserTimeoutCall(_0x289ece){if(_0x289ece['callee']['type']===nodeTypes['memberExpression']&&_0x289ece['callee']['property']['type']===nodeTypes['identifier']){const _0x13928e=_0x289ece['callee']['object']['type']===nodeTypes['identifier']&&(_0x289ece['callee']['object']['name']===constants['functionNames']['page']||_0x289ece['callee']['object']['name']==='browser');return _0x13928e&&getMethodNameFromCallee(_0x289ece['callee'])===constants['functionNames']['setDefaultTimeout'];}return ![];}__name(isBrowserTimeoutCall,'isBrowserTimeoutCall');function isFunctionWithWaitInName(_0x661b97){let _0x25b50e=![];if(_0x661b97['callee']['type']===nodeTypes['identifier'])return _0x661b97['callee']['name']['toLowerCase']()['includes'](constants['waitPrefix']);return _0x661b97['callee']['type']===nodeTypes['memberExpression']&&_0x661b97['callee']['property']['type']===nodeTypes['identifier']&&(_0x25b50e=_0x661b97['callee']['property']['name']['toLowerCase']()['includes'](constants['waitPrefix'])),_0x25b50e;}__name(isFunctionWithWaitInName,'isFunctionWithWaitInName');function isWithinReportedParent(_0x31a51f,_0x5359a5){let _0x5ddf34=_0x31a51f['parent'];while(_0x5ddf34){if(_0x5359a5['has'](_0x5ddf34))return !![];_0x5ddf34=_0x5ddf34['parent'];}return ![];}__name(isWithinReportedParent,'isWithinReportedParent');function containsDelayPattern(_0x877660,_0x1e57bd){let _0x1e8678=![];if(_0x877660&&_0x877660['type']===nodeTypes['callExpression']){if(_0x877660['callee']['type']===nodeTypes['identifier']&&_0x877660['callee']['name']===constants['functionNames']['promise']){const _0x3943ed=_0x1e57bd??'';return _0x3943ed['includes'](constants['functionNames']['setTimeout'])||_0x3943ed['includes'](runtimeStaticMethods['resolve'])&&_0x3943ed['includes'](',');}_0x877660['callee']['type']===nodeTypes['identifier']&&_0x877660['callee']['name']===constants['functionNames']['setTimeout']&&(_0x1e8678=!![]),_0x877660['callee']['type']===nodeTypes['memberExpression']&&_0x877660['callee']['property']['type']===nodeTypes['identifier']&&isPlaywrightWaitMethod(_0x877660['callee']['property']['name'])&&!_0x1e8678&&(_0x1e8678=!![]);}return _0x1e8678;}__name(containsDelayPattern,'containsDelayPattern');function getBodyStatements(_0x2a83ba){if(_0x2a83ba['body'])return _0x2a83ba['body']['type']===nodeTypes['blockStatement']?_0x2a83ba['body']['body']:[_0x2a83ba['body']];return [];}__name(getBodyStatements,'getBodyStatements');function checkStatementForDelays(_0x21ea8f){switch(_0x21ea8f['type']){case nodeTypes['blockStatement']:return _0x21ea8f['body']['some'](checkNodeForDelays);case nodeTypes['expressionStatement']:return checkNodeForDelays(_0x21ea8f['expression']);case nodeTypes['returnStatement']:return _0x21ea8f['argument']?checkNodeForDelays(_0x21ea8f['argument']):![];case nodeTypes['ifStatement']:return checkNodeForDelays(_0x21ea8f['consequent'])||(_0x21ea8f['alternate']?checkNodeForDelays(_0x21ea8f['alternate']):![]);default:return ![];}}__name(checkStatementForDelays,'checkStatementForDelays');function checkExpressionForDelays(_0x27e616){switch(_0x27e616['type']){case nodeTypes['callExpression']:return containsDelayPattern(_0x27e616)||_0x27e616['arguments']['some'](checkNodeForDelays);case nodeTypes['newExpression']:return containsDelayPattern(_0x27e616)||(_0x27e616['arguments']?.['some'](checkNodeForDelays)??![]);case nodeTypes['arrowFunctionExpression']:case nodeTypes['functionExpression']:return containsDelayInFunction(_0x27e616);default:return ![];}}__name(checkExpressionForDelays,'checkExpressionForDelays');function checkNodeForDelays(_0x2e15b7){let _0x51d982=![];return (containsDelayPattern(_0x2e15b7)||checkStatementForDelays(_0x2e15b7)||checkExpressionForDelays(_0x2e15b7))&&(_0x51d982=!![]),_0x51d982;}__name(checkNodeForDelays,'checkNodeForDelays');function containsDelayInFunction(_0x4ae27e){return getBodyStatements(_0x4ae27e)['some'](checkNodeForDelays);}__name(containsDelayInFunction,'containsDelayInFunction');function reportOnce(_0x3fdf51,_0x1a14b7,_0x1ff205){if(_0x1ff205['has'](_0x1a14b7))return;_0x1ff205['add'](_0x1a14b7),_0x3fdf51['report']({'node':_0x1a14b7,'messageId':constants['messageIds']['noDelaysInTests']});}__name(reportOnce,'reportOnce');function handleCallExpression(_0x11ab87,_0x490d24,_0x2e2aa1,_0x130bf8){if(!_0x2e2aa1||isWithinReportedParent(_0x11ab87,_0x130bf8))return;let _0x581a1d=![];(isDirectSetTimeoutCall(_0x11ab87)||isPlaywrightWaitCall(_0x11ab87)||isBrowserTimeoutCall(_0x11ab87))&&(_0x581a1d=!![]);if(!_0x581a1d&&!isFunctionWithWaitInName(_0x11ab87)){const _0x10feee=_0x11ab87['callee'];_0x10feee['type']===nodeTypes['identifier']&&constants['delayFunctionPatterns']['some'](_0x12a29d=>_0x10feee['name']['toLowerCase']()['includes'](_0x12a29d))&&(_0x581a1d=!![]);}_0x581a1d&&reportOnce(_0x490d24,_0x11ab87,_0x130bf8);}__name(handleCallExpression,'handleCallExpression');function handleNewExpression(_0x1dfa58,_0x129456,_0x3bca11,_0x129cbc){if(!_0x3bca11||isWithinReportedParent(_0x1dfa58,_0x129cbc))return;if(_0x1dfa58['callee']['type']===nodeTypes['identifier']&&_0x1dfa58['callee']['name']===constants['functionNames']['promise']){const _0x5db212=_0x129456['sourceCode']['getText'](_0x1dfa58);(_0x5db212['includes'](constants['functionNames']['setTimeout'])||_0x5db212['includes'](runtimeStaticMethods['resolve'])&&_0x5db212['includes'](','))&&reportOnce(_0x129456,_0x1dfa58,_0x129cbc);}}__name(handleNewExpression,'handleNewExpression');function checkIsPlaywrightFile(_0x22c51c){const _0x4576d9=_0x22c51c['filename'];if(!isTestFile(_0x4576d9))return ![];return isPlaywrightImportOrUsage(_0x22c51c['sourceCode']['getText']());}__name(checkIsPlaywrightFile,'checkIsPlaywrightFile');function handleFunction(_0x50f5d4,_0x3f3475,_0xc04f2,_0x2d9a21){_0xc04f2&&!isWithinReportedParent(_0x50f5d4,_0x2d9a21)&&containsDelayInFunction(_0x50f5d4)&&reportOnce(_0x3f3475,_0x50f5d4,_0x2d9a21);}__name(handleFunction,'handleFunction');function isIdentifier(_0x39be48){return _0x39be48['type']===nodeTypes['identifier'];}__name(isIdentifier,'isIdentifier');function handleVariableDeclarator(_0x37703b,_0x4ce634,_0x203917,_0x4c98cc){if(!_0x203917||!_0x37703b['id']||isWithinReportedParent(_0x37703b,_0x4c98cc))return;if(!isIdentifier(_0x37703b['id']))return;const _0x573773=_0x37703b['id'];if(!_0x573773['name']['toLowerCase']()['includes'](constants['waitPrefix'])){if(constants['delayFunctionPatterns']['some'](_0x881581=>_0x573773['name']['toLowerCase']()['includes'](_0x881581))){reportOnce(_0x4ce634,_0x37703b,_0x4c98cc);return;}_0x37703b['init']&&(_0x37703b['init']['type']===nodeTypes['arrowFunctionExpression']||_0x37703b['init']['type']===nodeTypes['functionExpression'])&&containsDelayInFunction(_0x37703b['init'])&&reportOnce(_0x4ce634,_0x37703b,_0x4c98cc);}}__name(handleVariableDeclarator,'handleVariableDeclarator');function checkFunctionNode(_0x46873d,_0x2afcca,_0x1c27f5,_0xaf496a){switch(_0x46873d['type']){case nodeTypes['callExpression']:handleCallExpression(_0x46873d,_0x2afcca,_0x1c27f5,_0xaf496a);break;case nodeTypes['newExpression']:handleNewExpression(_0x46873d,_0x2afcca,_0x1c27f5,_0xaf496a);break;case nodeTypes['functionDeclaration']:case nodeTypes['arrowFunctionExpression']:case nodeTypes['functionExpression']:handleFunction(_0x46873d,_0x2afcca,_0x1c27f5,_0xaf496a);break;case nodeTypes['variableDeclarator']:handleVariableDeclarator(_0x46873d,_0x2afcca,_0x1c27f5,_0xaf496a);break;default:break;}}__name(checkFunctionNode,'checkFunctionNode');

export { noDelaysInUiTests as default, ruleName };
