'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { eslintFixTypeNames } from '../../../base-repo/constants/internal-common-strings.js';
import { createNodeVisitor, nodeTypes, hasUndefined, hasNull, forEachUnionTypedParam } from '@flint/infra/analysis';
import { ruleTypes, url } from '@flint/infra/constants/rules-meta';
import { first } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x5c5472,_0x12c2e7)=>__defProp(_0x5c5472,'name',{'value':_0x12c2e7,'configurable':!![]});const ruleName='no-use-type-undefined-for-optional';const ruleConstants={'messageId':{'noUseUndefinedForOptional':'noUseUndefinedForOptional'},'meta':{'type':ruleTypes['problem'],'docs':{'description':'Disallow\x20usage\x20of\x20`undefined`\x20|\x20`null`\x20in\x20parameter\x20types.\x20Use\x20optional\x20parameters\x20instead.'},'messages':{'noUseUndefinedForOptional':'no-use-type-undefined-for-optional\x0aParameter\x20\x27{{paramName}}\x27\x20must\x20not\x20use\x20`undefined`\x20or\x20`null`\x20in\x20its\x20type.\x20Use\x20optional\x20parameter\x20syntax\x20(`param?:\x20Type`)\x20instead.\x0a\x0a###\x20**Must\x20not**\x20use\x20type\x20undefined\x20or\x20null\x20for\x20optional\x20parameters\x20in\x20functions.\x0a\x0a####\x20Wrong\x0a\x0afunction\x20foo(\x0a\x20\x20\x20\x20bar:\x20string\x20|\x20undefined,\x0a\x20\x20\x20\x20baz:\x20number)\x20{\x0a\x20\x20\x20\x20...\x0a}\x0a\x20\x20\x20\x20function\x20foo(\x0a\x20\x20\x20\x20bar:\x20string\x20|\x20null,\x0a\x20\x20\x20\x20baz:\x20number)\x20{\x0a\x20\x20\x20\x20...\x0a}\x0a\x0a####\x20Correct\x0a\x0afunction\x20foo(\x0a\x20\x20\x20\x20baz:\x20number,\x0a\x20\x20\x20\x20bar?:\x20string)\x20{\x0a\x20\x20\x20\x20...\x0a}\x0a\x0a####\x20Why\x0a\x0aThis\x20makes\x20your\x20code\x20clearer\x20and\x20improves\x20readability.'},'schema':[],'fixable':eslintFixTypeNames['code']}},createRule=ESLintUtils['RuleCreator'](_0x181553=>''+url+_0x181553),noUseUndefinedForOptional=createRule({'name':ruleName,'meta':ruleConstants['meta'],'defaultOptions':[],'create':__name(_0x33f62a=>{return {...createNodeVisitor(_0x51694f=>handleNodes(_0x33f62a,_0x51694f),[nodeTypes['functionDeclaration'],nodeTypes['functionExpression'],nodeTypes['arrowFunctionExpression'],nodeTypes['TSMethodSignature'],nodeTypes['TSTypeAliasDeclaration'],nodeTypes['TSTypeAnnotation'],nodeTypes['TSPropertySignature']])};},'create')});function createAutoFix(_0x27d3f2,_0x17f15c,_0x1c3d52,_0x184ab4){let _0xf1583a=null;const _0x2f9a5c=_0x184ab4['types']['filter'](_0x5d68ea=>_0x5d68ea['type']!==nodeTypes['TSUndefinedKeyword']);if(_0x2f9a5c['length']>0x1*-0x111a+0x3*0x976+0x13*-0x98){let _0x445f95;if(_0x2f9a5c['length']===-0x6*-0x4a3+-0x2e*0x6b+-0x897)_0x445f95=_0x27d3f2['getText'](first(_0x2f9a5c));else {const _0x15d1af=_0x2f9a5c['map'](_0x9276e1=>_0x27d3f2['getText'](_0x9276e1));_0x445f95=_0x15d1af['join']('\x20|\x20');}const _0x599712=[first(_0x1c3d52['range']),_0x184ab4['range'][0x427+-0xf2b+0x193*0x7]];_0xf1583a=_0x17f15c['replaceTextRange'](_0x599712,_0x1c3d52['name']+'?:\x20'+_0x445f95);}return _0xf1583a;}__name(createAutoFix,'createAutoFix');function checkTypeAnnotations(_0xe8287b,_0x5c8616,_0x450837){const _0x5e4f10=hasUndefined(_0x450837['types']),_0x35da15=hasNull(_0x450837['types']);(_0x5e4f10||_0x35da15)&&_0xe8287b['report']({'node':_0x5c8616,'messageId':ruleConstants['messageId']['noUseUndefinedForOptional'],'data':{'paramName':_0x5c8616['name']},'fix'(_0x46917e){let _0x29520f=null;return _0x5e4f10&&(_0x29520f=createAutoFix(_0xe8287b['sourceCode'],_0x46917e,_0x5c8616,_0x450837)),_0x29520f;}});}__name(checkTypeAnnotations,'checkTypeAnnotations');function handleNodes(_0x230864,_0x573c5c){const _0x335c1a=__name((_0x59c62c,_0x5d762d)=>checkTypeAnnotations(_0x230864,_0x59c62c,_0x5d762d),'check');switch(_0x573c5c['type']){case nodeTypes['functionDeclaration']:case nodeTypes['functionExpression']:case nodeTypes['arrowFunctionExpression']:case nodeTypes['TSMethodSignature']:forEachUnionTypedParam(_0x573c5c['params'],_0x335c1a);break;case nodeTypes['TSTypeAliasDeclaration']:case nodeTypes['TSTypeAnnotation']:_0x573c5c['typeAnnotation']['type']===nodeTypes['TSFunctionType']&&forEachUnionTypedParam(_0x573c5c['typeAnnotation']['params'],_0x335c1a);break;case nodeTypes['TSPropertySignature']:{const _0x1ed480=_0x573c5c['key'];if(_0x1ed480['type']===nodeTypes['identifier']){const _0x1e9ad7=_0x573c5c['typeAnnotation']?.['typeAnnotation'];_0x1e9ad7&&_0x1e9ad7['type']===nodeTypes['TSUnionType']&&_0x335c1a(_0x1ed480,_0x1e9ad7);}break;}default:break;}}__name(handleNodes,'handleNodes');

export { noUseUndefinedForOptional as default, ruleName };
