'use strict';
import { ESLintUtils } from '@typescript-eslint/utils';
import { getStringLiterals, nodeTypes, isBinaryExpression, getFunctionName, isEnvironmentVariableAssignment, getCalleeName } from '@flint/infra/analysis';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { htmlAtributes } from '../../../base-repo/constants/html-element-names.js';
import { eslintProperties, binaryOperators, ruleTypes, url } from '@flint/infra/constants/rules-meta';
import { runtimeConstructors } from '../../../base-repo/constants/runtime-constants.js';
import { flintRuleIdentifiers } from '@flint/infra/constants/shared-string-identifiers';
import { urlProtocols } from '../../../base-repo/constants/http-constants.js';
import { commonLowerCaseWords } from '../../../base-repo/constants/internal-common-strings.js';

var __defProp=Object['defineProperty'],__name=(_0x506b99,_0x3e55e0)=>__defProp(_0x506b99,'name',{'value':_0x3e55e0,'configurable':!![]});const ruleName='must-use-new-url-constructor';const constants={'messageIds':{'mustUseNewUrlConstructor':'mustUseNewUrlConstructor'},'meta':{'type':ruleTypes['suggestion'],'schema':[]},'maxRecursionDepth':0x4,'plus':binaryOperators['plus'],'newUrlIdentifier':runtimeConstructors['url'],'protocols':[urlProtocols['http'],urlProtocols['https'],'ws://','wss://','ftp://'],'urlPatterns':['apiUrl',htmlAtributes['href'],'siteUrl','serverUrl','hostUrl'],'urlFunctions':['getUrl','getBaseUrl'],'pathPrefixes':['./','../','//'],'blockedSubstrings':['/'+folders['api']+'/'],'localHosts':['localhost','127.0.0.1'],'urlAttributes':[htmlAtributes['href'],folders['src'],'action','formAction','srcSet'],'nonWebFunctionNames':[flintRuleIdentifiers['ruleCreator'],flintRuleIdentifiers['log'],'debug','createError','logInfo','normalizeUrl','getMailToUrl','getRelativeUrl','addSlashToUrlIfNeeded','getAbsoluteUrl'],'nonWebProtocols':[eslintProperties['mailto'],'tel:','sms:','data:'],'dotUrl':'.'+commonLowerCaseWords['url']},createRule=ESLintUtils['RuleCreator'](_0x16220=>''+url+_0x16220),mustUseNewUrlConstructor=createRule({'name':ruleName,'meta':{'type':constants['meta']['type'],'docs':{'description':'Must\x20use\x20new\x20URL()\x20for\x20URL\x20constructor\x20instead\x20of\x20manual\x20string\x20concatenation'},'messages':{[constants['messageIds']['mustUseNewUrlConstructor']]:'must-use-new-url-constructor\x0a###\x20**Must**\x20use\x20new\x20URL()\x20for\x20URL\x20constructor\x20and\x20use\x20URL\x20constructor\x20instead\x20of\x20constructing\x20URL\x20with\x20\x22/\x22\x20symbols\x20manually.\x0a\x0a####\x20Wrong\x0a\x0aconst\x20foo\x20=\x20getBaseUrl()\x20+\x20\x22/\x22\x20+\x20endPoint;\x0a\x0a####\x20Correct\x0a\x0aconst\x20foo\x20=\x20new\x20URL(endPoint,\x20getBaseUrl()).href;\x0a\x0a####\x20Why\x0a\x0aIt\x20ensures\x20proper\x20URL\x20construction\x20by\x20handling\x20edge\x20cases\x20such\x20as\x20trailing\x20slashes\x20and\x20encoding\x20issues.\x20The\x20new\x20URL(id,\x20path).href\x20approach\x20is\x20more\x20robust\x20and\x20reliable\x20compared\x20to\x20manually\x20concatenating\x20strings,\x20which\x20can\x20lead\x20to\x20incorrect\x20URLs.'},'schema':constants['meta']['schema']},'defaultOptions':[],'create'(_0x42b8ba){const _0x484d2d=new Set();return {'BinaryExpression'(_0x485afe){if(_0x484d2d['has'](_0x485afe)||!isStringConcatenation(_0x485afe['operator'])||isArgumentOfNewURL(_0x485afe)||isFilePath(_0x485afe)||isInNonWebContext(_0x485afe)||isTextContent(_0x485afe))return;isLikelyURL(_0x485afe)&&(addToReported(_0x485afe,_0x484d2d),_0x42b8ba['report']({'node':_0x485afe,'messageId':constants['messageIds']['mustUseNewUrlConstructor']}));},'TemplateLiteral'(_0x27682d){if(!hasExpressions(_0x27682d['expressions'])||isArgumentOfNewURL(_0x27682d)||isFilePath(_0x27682d)||isInNonWebContext(_0x27682d)||isTextContent(_0x27682d))return;isLikelyURL(_0x27682d)&&_0x42b8ba['report']({'node':_0x27682d,'messageId':constants['messageIds']['mustUseNewUrlConstructor']});}};}});function isLocalUrl(_0xab5fa0){const _0x52f5fc=getStringLiterals(_0xab5fa0);return _0x52f5fc['some'](_0x192fe0=>constants['localHosts']['some'](_0x5714a8=>_0x192fe0['includes'](_0x5714a8)));}__name(isLocalUrl,'isLocalUrl');function hasNonWebProtocol(_0x567b06){const _0x200bdc=getStringLiterals(_0x567b06);return _0x200bdc['some'](_0x59b0ec=>constants['nonWebProtocols']['some'](_0x1bbad8=>_0x59b0ec['startsWith'](_0x1bbad8)));}__name(hasNonWebProtocol,'hasNonWebProtocol');function isTextContent(_0x2bf42f){const _0x20d065=getStringLiterals(_0x2bf42f);return _0x20d065['some'](_0xe53ca0=>_0xe53ca0['includes']('\x0a')||/\s/['test'](_0xe53ca0['trim']()));}__name(isTextContent,'isTextContent');function isArgumentOfNewURL(_0x1f0afb){const _0x180149=_0x1f0afb['parent'];return _0x180149?.['type']===nodeTypes['newExpression']&&_0x180149['callee']['type']===nodeTypes['identifier']&&_0x180149['callee']['name']===constants['newUrlIdentifier']&&_0x180149['arguments']['includes'](_0x1f0afb);}__name(isArgumentOfNewURL,'isArgumentOfNewURL');function addToReported(_0x45381c,_0x327be9,_0x5e2ac2=0x13d1+0x1a4f+-0x2e20){if(_0x5e2ac2>0x199c+-0x4fc+-0x149c)return;if(isBinaryExpression(_0x45381c)){const _0x1605a5=_0x45381c;_0x327be9['add'](_0x1605a5),addToReported(_0x1605a5['left'],_0x327be9,_0x5e2ac2+(0x1c2+0x43*0x71+-0x1f54)),addToReported(_0x1605a5['right'],_0x327be9,_0x5e2ac2+(0x245*-0xb+0x2*-0x301+0x1efa));}}__name(addToReported,'addToReported');function isFilePath(_0x45ef26){const _0x3f86c1=getStringLiterals(_0x45ef26);return _0x3f86c1['some'](_0x57359f=>constants['pathPrefixes']['some'](_0x39d67a=>_0x57359f['startsWith'](_0x39d67a)));}__name(isFilePath,'isFilePath');function hasUrlProtocols(_0x1d84f3){const _0x1bdf82=getStringLiterals(_0x1d84f3);return _0x1bdf82['some'](_0x2b49f7=>constants['protocols']['some'](_0xc9ff63=>_0x2b49f7['includes'](_0xc9ff63)));}__name(hasUrlProtocols,'hasUrlProtocols');function hasUrlVariableNames(_0x18703b){const _0x54f441=getAllIdentifiers(_0x18703b),_0x4105f3=getStringLiterals(_0x18703b),_0x302243=_0x54f441['some'](_0x54dfd6=>constants['urlPatterns']['some'](_0x30fd34=>_0x54dfd6['toLowerCase']()===_0x30fd34['toLowerCase']()||_0x54dfd6['toLowerCase']()['endsWith'](_0x30fd34['toLowerCase']()))),_0x19ba73=_0x54f441['some'](_0x24b9ae=>_0x24b9ae['toLowerCase']()['endsWith'](constants['dotUrl']))&&_0x4105f3['some'](_0x116fbd=>_0x116fbd['startsWith']('/'));return _0x302243||_0x19ba73;}__name(hasUrlVariableNames,'hasUrlVariableNames');function collectIdentifiers(_0x411a93,_0x5bb9fc){_0x5bb9fc['type']===nodeTypes['identifier']&&_0x411a93['push'](_0x5bb9fc['name']),_0x5bb9fc['type']===nodeTypes['memberExpression']&&_0x5bb9fc['object']['type']===nodeTypes['identifier']&&_0x5bb9fc['property']['type']===nodeTypes['identifier']&&!_0x5bb9fc['computed']&&_0x411a93['push'](_0x5bb9fc['object']['name']+'.'+_0x5bb9fc['property']['name']);}__name(collectIdentifiers,'collectIdentifiers');function getAllIdentifiers(_0x15056a){const _0x49edb8=[];if(isBinaryExpression(_0x15056a)){if(_0x15056a['left']['type']!==nodeTypes['literal'])collectIdentifiers(_0x49edb8,_0x15056a['left']);if(_0x15056a['right']['type']!==nodeTypes['literal'])collectIdentifiers(_0x49edb8,_0x15056a['right']);}else _0x15056a['expressions']['forEach'](_0x16d1ba=>collectIdentifiers(_0x49edb8,_0x16d1ba));return _0x49edb8;}__name(getAllIdentifiers,'getAllIdentifiers');function hasUrlFunctionCalls(_0x1bdfbc){const _0x52f662=getDirectFunctionCalls(_0x1bdfbc);return _0x52f662['some'](_0x51c268=>constants['urlFunctions']['some'](_0x8ccda5=>_0x51c268['toLowerCase']()['includes'](_0x8ccda5['toLowerCase']())));}__name(hasUrlFunctionCalls,'hasUrlFunctionCalls');function getDirectFunctionCalls(_0x7610e2){const _0x1e2a4f=[];if(isBinaryExpression(_0x7610e2)){if(_0x7610e2['left']['type']===nodeTypes['callExpression']){const _0x40cc28=getFunctionName(_0x7610e2['left']);if(_0x40cc28)_0x1e2a4f['push'](_0x40cc28);}if(_0x7610e2['right']['type']===nodeTypes['callExpression']){const _0x118338=getFunctionName(_0x7610e2['right']);if(_0x118338)_0x1e2a4f['push'](_0x118338);}}else _0x7610e2['expressions']['forEach'](_0x80fd3e=>{if(_0x80fd3e['type']===nodeTypes['callExpression']){const _0x2cb9d9=getFunctionName(_0x80fd3e);if(_0x2cb9d9)_0x1e2a4f['push'](_0x2cb9d9);}});return _0x1e2a4f;}__name(getDirectFunctionCalls,'getDirectFunctionCalls');function hasWebContext(_0x5b8396){const _0x40647b=getStringLiterals(_0x5b8396),_0x194ce3=getAllIdentifiers(_0x5b8396),_0x5d18a8=/^[^\s]*\.(com|org|net|io|dev|app|site)(\/[^\s]*)?$/,_0x208422=/(apiUrl|hostUrl|serverUrl|graphql)/i;if(isLocalUrl(_0x5b8396)||_0x40647b['some'](_0x4e422b=>constants['blockedSubstrings']['some'](_0x3bfb31=>_0x4e422b['includes'](_0x3bfb31))||_0x5d18a8['test'](_0x4e422b)))return !![];if(_0x194ce3['some'](_0x424925=>_0x208422['test'](_0x424925)))return !![];return ![];}__name(hasWebContext,'hasWebContext');function isInJSXUrlAttribute(_0x5df6cd){let _0xd991e1=_0x5df6cd['parent'],_0x1cd8a8=![];while(_0xd991e1&&_0xd991e1['type']!==nodeTypes['JSXExpressionContainer']){_0xd991e1=_0xd991e1['parent'];}if(!_0xd991e1||_0xd991e1['type']!==nodeTypes['JSXExpressionContainer'])return _0x1cd8a8;const _0x4de5ce=_0xd991e1?.['parent'];if(_0x4de5ce&&_0x4de5ce['type']===nodeTypes['JSXAttribute']){const _0x12bb06=_0x4de5ce['name'];_0x12bb06&&_0x12bb06['type']===nodeTypes['JSXIdentifier']&&(_0x1cd8a8=constants['urlAttributes']['some'](_0x5a1a92=>_0x5a1a92['toLowerCase']()===_0x12bb06['name']['toLowerCase']()));}return _0x1cd8a8;}__name(isInJSXUrlAttribute,'isInJSXUrlAttribute');function isLikelyURL(_0x1bc156){return (hasUrlProtocols(_0x1bc156)||hasUrlVariableNames(_0x1bc156)||hasUrlFunctionCalls(_0x1bc156)||hasWebContext(_0x1bc156)||isInJSXUrlAttribute(_0x1bc156))&&!isEnvironmentVariableAssignment(_0x1bc156)&&!hasNonWebProtocol(_0x1bc156);}__name(isLikelyURL,'isLikelyURL');function isInNonWebContext(_0x5d8e71){let _0x50172a=_0x5d8e71;while(_0x50172a){if(_0x50172a['type']===nodeTypes['callExpression']){const _0x3fe8c9=getCalleeName(_0x50172a['callee']);if(_0x3fe8c9&&constants['nonWebFunctionNames']['some'](_0x4953f6=>_0x4953f6['toLowerCase']()===_0x3fe8c9['toLowerCase']()))return !![];}_0x50172a=_0x50172a['parent'];}return ![];}__name(isInNonWebContext,'isInNonWebContext');function isStringConcatenation(_0x1bbcc8){return _0x1bbcc8==='+';}__name(isStringConcatenation,'isStringConcatenation');function hasExpressions(_0x11f798){return _0x11f798['length']>-0x25*0x40+0x1c*-0x55+0x128c;}__name(hasExpressions,'hasExpressions');

export { mustUseNewUrlConstructor as default, ruleName };
