'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { getIndentSize, nodeTypes, isContainSideEffectExpr, isExprNeedsBrackets } from '@flint/infra/analysis';
import { eslintFixTypeNames } from '../../../base-repo/constants/internal-common-strings.js';
import { ruleTypes, logicalOperators, url } from '@flint/infra/constants/rules-meta';
import { firstOrUndefined } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x210140,_0x517428)=>__defProp(_0x210140,'name',{'value':_0x517428,'configurable':!![]});const ruleName='must-consolidate-conditional-expressions';const constants={'messageId':'mustConsolidateConditionalExpressions','andOperator':logicalOperators['and'],'meta':{'type':ruleTypes['suggestion'],'docs':{'description':'Avoid\x20nested\x20conditions,\x20combine\x20them\x20with\x20logical\x20operators\x20when\x20possible'},'fixable':eslintFixTypeNames['code'],'schema':[],'messages':{'mustConsolidateConditionalExpressions':ruleName+'\x0aConsider\x20replacing\x20it\x20with\x20this:\x20\x27if\x20({{combinedCondition}})\x27.\x0a\x0a###\x20**Must**\x20combine\x20nested\x20conditions\x20using\x20logical\x20operators.\x0a\x0a####\x20Wrong\x0a\x0aif\x20(a)\x20{\x0a\x20\x20\x20\x20if\x20(b)\x20{\x0a\x20\x20\x20\x20\x20\x20\x20\x20someFunction();\x0a\x20\x20\x20\x20}\x0a}\x0a\x0a####\x20Correct\x0a\x0aif\x20(a\x20&&\x20b)\x20{\x0a\x20\x20\x20\x20someFunction();\x0a};\x0a\x0a####\x20Why\x0a\x0aFlattening\x20nested\x20conditions\x20improves\x20readability\x20and\x20reduces\x20unnecessary\x20indentation.'}}},createRule=ESLintUtils['RuleCreator'](_0x2f036a=>''+url+_0x2f036a),mustConsolidateConditionalExpressions=createRule({'name':ruleName,'meta':constants['meta'],'defaultOptions':[],'create'(_0x59ff14){const _0x30102b=_0x59ff14['sourceCode'],_0x1cc731=getIndentSize(_0x30102b);return {'IfStatement'(_0x16df40){if(_0x16df40['alternate'])return;let _0x43124c;if(_0x16df40['consequent']['type']===nodeTypes['ifStatement'])_0x43124c=_0x16df40['consequent'];else _0x16df40['consequent']['type']===nodeTypes['blockStatement']&&(_0x43124c=getSingleIfStatement(_0x16df40['consequent']));if(_0x43124c){const _0x5ba8dc=!_0x43124c['alternate']&&!isContainSideEffectExpr(_0x43124c['test']);if(_0x5ba8dc){const {depth:_0x8794a0,combinedCondition:_0x5092cf,finalBody:_0x2bc06c}=mergeConditions(_0x16df40,_0x30102b);_0x59ff14['report']({'node':_0x16df40,'messageId':constants['messageId'],'data':{'combinedCondition':_0x5092cf},'fix'(_0x7439ba){let _0x436219;if(_0x2bc06c['type']===nodeTypes['blockStatement']){const _0x1814d9=_0x30102b['getText'](_0x2bc06c)['split']('\x0a'),_0x29a224=_0x1814d9['map'](_0x51b01f=>{if(_0x51b01f['startsWith'](_0x1cc731['repeat'](_0x8794a0-(-0x18e9+0x77*0x11+0x1103))))return _0x51b01f['slice']((_0x8794a0-(-0x2c3*-0x1+-0xe5+-0x35*0x9))*_0x1cc731['length']);else return _0x51b01f;});_0x436219=_0x29a224['join']('\x0a');}else _0x436219=_0x30102b['getText'](_0x2bc06c);const _0x57e5d2='if\x20('+_0x5092cf+')\x20'+_0x436219;return _0x7439ba['replaceText'](_0x16df40,_0x57e5d2);}});}}}};}});function formatCondition(_0x23aa1f,_0x10bc7c){const _0x23071c=_0x10bc7c['getText'](_0x23aa1f);return isExprNeedsBrackets(_0x23aa1f)?'('+_0x23071c+')':_0x23071c;}__name(formatCondition,'formatCondition');function getSingleIfStatement(_0x3eddf5){let _0x16c4eb;if(_0x3eddf5['body']['length']===-0xf3e+-0x1327*-0x1+0x32*-0x14){const _0x32090=firstOrUndefined(_0x3eddf5['body']);_0x32090&&_0x32090['type']===nodeTypes['ifStatement']&&(_0x16c4eb=_0x32090);}return _0x16c4eb;}__name(getSingleIfStatement,'getSingleIfStatement');function mergeConditions(_0x2922de,_0x5b518e){const _0x3bc769=[];let _0x67fb1f=_0x2922de['consequent'],_0x3fcada=_0x2922de['test'],_0x45b799=-0x2309+-0x1*-0x270e+-0x404;_0x3bc769['push'](formatCondition(_0x3fcada,_0x5b518e));while(!![]){let _0x47bbf1;if(_0x67fb1f['type']===nodeTypes['ifStatement']){if(_0x67fb1f['alternate'])break;_0x47bbf1=_0x67fb1f;}else {if(_0x67fb1f['type']===nodeTypes['blockStatement']){const _0x34f3a4=getSingleIfStatement(_0x67fb1f);if(!_0x34f3a4||_0x34f3a4['alternate'])break;_0x47bbf1=_0x34f3a4;}else break;}if(isContainSideEffectExpr(_0x47bbf1['test']))break;_0x3bc769['push'](formatCondition(_0x47bbf1['test'],_0x5b518e)),_0x67fb1f=_0x47bbf1['consequent'],_0x45b799++;}return {'combinedCondition':_0x3bc769['join']('\x20'+constants['andOperator']+'\x20'),'finalBody':_0x67fb1f,'depth':_0x45b799};}__name(mergeConditions,'mergeConditions');

export { mustConsolidateConditionalExpressions as default, ruleName };
