'use strict';
import { createEslintRule } from '@flint/infra/create-eslint-rule';
import { normalizePathSeparators, nodeTypes, hasJsDocComment } from '@flint/infra/analysis';
import _0x2de5a4__default from 'typescript';
import { aliases } from '../../../base-repo/app-constants/alias-symbols.js';
import { fileExtensions } from '../../../base-repo/app-constants/internal-system-constants.js';
import { folders, projectPaths, packagesPath } from '../../../base-repo/app-constants/project-paths.js';
import { existsSync, readFileSync } from 'node:fs';
import _0x1c3e3a from 'node:path';
import { flintRuleIdentifiers } from '@flint/infra/constants/shared-string-identifiers';

var __defProp=Object['defineProperty'],__name=(_0x537c9c,_0x4d70a0)=>__defProp(_0x537c9c,'name',{'value':_0x4d70a0,'configurable':!![]});const ruleName='must-mark-external-imports-as-internal';const packagesMarkerRelative=''+folders['packages']+projectPaths['root'],packagesMarkerAbsolute=''+projectPaths['root']+packagesMarkerRelative,fileExtentions=[fileExtensions['ts'],fileExtensions['tsx'],fileExtensions['dts'],fileExtensions['js'],fileExtensions['mjs'],fileExtensions['cjs']],ruleContent='\x0a###\x20**Must**\x20import\x20from\x20other\x20NX\x20projects\x20only\x20if\x20one\x20of\x20the\x20following\x20is\x20true.\x0a1.\x20imported\x20member\x20name\x20starts\x20with\x20\x27internal\x27\x0a2.\x20source\x20file\x20name\x20starts\x20with\x20\x27internal\x27\x0a3.\x20imported\x20symbol\x20has\x20JSDoc\x20comment\x20(/**\x20*/),\x20e.g.\x20@monorepo-internal\x20or\x20@internal\x0a\x0a####\x20Wrong\x0a\x0aimport\x20{\x20foo\x20}\x20from\x20\x22@fd-toolbox/constants/resource-id-constants\x22;\x0a\x0a####\x20Correct\x0a\x0aimport\x20{\x20foo\x20}\x20from\x20\x22@scope/lib/internal-utils\x22;\x0a\x0a/**\x20@monorepo-internal\x20*/\x0aexport\x20const\x20foo\x20=\x20()\x20=>\x20{};\x0a\x0aimport\x20{\x20internalFoo\x20}\x20from\x20\x22@scope/lib/utils\x22;\x0a\x0a####\x20Why\x0a\x0aFor\x20cross-project\x20imports\x20this\x20protects\x20non-internal\x20modules\x20from\x20accidental\x20usage.\x0aUse\x20names\x20starting\x20with\x20\x27internal\x27,\x20source\x20files\x20that\x20start\x20with\x20\x27internal\x27,\x0aor\x20exports\x20marked\x20with\x20allowed\x20internal\x20JSDoc\x20comments.',nxAliasProjectNames=Object['values'](aliases)['filter'](_0x5bcf68=>_0x5bcf68['startsWith']('@')&&!!_0x5bcf68[0x5f9+0x29+0x3*-0x20b])['map'](_0x11d6e6=>_0x11d6e6['slice'](-0x241*0x7+0x1f*-0x8b+0x209d));var mustMarkExternalImportsAsInternal = createEslintRule(ruleName,ruleContent,_0x4875ad=>{const _0x2f18c6=normalizePathSeparators(_0x4875ad['filename']),_0x32f8c2=_0x4875ad['sourceCode']['parserServices'];if(_0x2f18c6['includes'](packagesPath['packagesFlint']))return {};return {'ImportDeclaration'(_0x989e95){const _0x4f137c=typeof _0x989e95['source']['value']==='string'?_0x989e95['source']['value']:void(0x778+0xc6b+0x3*-0x6a1);if(shouldSkipImportCheck(_0x2f18c6,_0x4f137c))return;const _0x3b3911=_0x32f8c2?.['program']?.['getTypeChecker'](),_0x34fd2c=_0x32f8c2?.[flintRuleIdentifiers['esTreeNodeToTSNodeMap']];for(const _0x4e190a of _0x989e95['specifiers']){let _0x1a6b11=![];_0x4f137c&&(_0x1a6b11=mustReportSpecifier(_0x4e190a,_0x4f137c,_0x2f18c6,_0x34fd2c,_0x3b3911)),_0x1a6b11&&_0x4875ad['report']({'node':_0x4e190a,'messageId':ruleName});}}};});function shouldSkipImportCheck(_0x1b1a51,_0x5f5646){return !_0x5f5646||!shouldCheckImportPath(_0x5f5646,_0x1b1a51)||hasInternalPrefixInSourceFileName(_0x5f5646);}__name(shouldSkipImportCheck,'shouldSkipImportCheck');function mustReportSpecifier(_0x5f2f15,_0x9fe820,_0x100205,_0x31abda,_0x9834ed){let _0x5c09e6=![];if(_0x5f2f15['type']===nodeTypes['importNamespaceSpecifier'])_0x5c09e6=!![];else {if(!isInternalNameSpecifier(_0x5f2f15)){const _0x5517cb=_0x9834ed?getImportedSymbol(_0x5f2f15,_0x31abda,_0x9834ed):void(-0x40*0x47+0xc5*0x1f+-0x61b),_0x499125=!!_0x5517cb&&_0x9834ed?hasJsDocComment(_0x5517cb,_0x9834ed):hasAnyJsDocInImportedModule(_0x5f2f15,_0x9fe820,_0x100205);_0x5c09e6=!_0x499125;}}return _0x5c09e6;}__name(mustReportSpecifier,'mustReportSpecifier');function hasAnyJsDocInImportedModule(_0x5bdd1d,_0x1b4baa,_0x438fc4){const _0x48a441=resolveImportFilePath(_0x1b4baa,_0x438fc4);if(!_0x48a441||!existsSync(_0x48a441))return ![];const _0x211719=readFileSync(_0x48a441,'utf8'),_0x3cc4c5=_0x2de5a4__default['createSourceFile'](_0x48a441,_0x211719,_0x2de5a4__default['ScriptTarget']['Latest'],!![]);return _0x5bdd1d['type']===nodeTypes['importSpecifier']?hasJsDocOnNamedExport(_0x3cc4c5['statements'],getImportedSpecifierName(_0x5bdd1d)):hasJsDocOnDefaultExport(_0x3cc4c5['statements']);}__name(hasAnyJsDocInImportedModule,'hasAnyJsDocInImportedModule');function hasJsDocOnNamedExport(_0x586f9c,_0x105d42){const _0x1a90ed=_0x586f9c['find'](_0x2dfc1a=>isNamedExportStatement(_0x2dfc1a,_0x105d42)),_0x2d3fe9=!!_0x1a90ed&&_0x2de5a4__default['getJSDocCommentsAndTags'](_0x1a90ed)['some'](_0x327974=>_0x2de5a4__default['isJSDoc'](_0x327974));return _0x2d3fe9;}__name(hasJsDocOnNamedExport,'hasJsDocOnNamedExport');function hasJsDocOnDefaultExport(_0x436fa3){const _0x4e5a9c=_0x436fa3['find'](_0x289f04=>{if(!isNodeExported(getNodeModifiers(_0x289f04)))return ![];return isNodeDefaultExport(getNodeModifiers(_0x289f04));}),_0x48ec31=!!_0x4e5a9c&&_0x2de5a4__default['getJSDocCommentsAndTags'](_0x4e5a9c)['some'](_0x1b60c0=>_0x2de5a4__default['isJSDoc'](_0x1b60c0));return _0x48ec31;}__name(hasJsDocOnDefaultExport,'hasJsDocOnDefaultExport');function isNamedExportStatement(_0x2c9cde,_0x373f9d){let _0x276771=![];if(isNodeExported(getNodeModifiers(_0x2c9cde))){_0x2de5a4__default['isVariableStatement'](_0x2c9cde)&&(_0x276771=_0x2c9cde['declarationList']['declarations']['some'](_0x3ad0da=>{return _0x2de5a4__default['isIdentifier'](_0x3ad0da['name'])&&_0x3ad0da['name']['text']===_0x373f9d;}));if(_0x2de5a4__default['isFunctionDeclaration'](_0x2c9cde)||_0x2de5a4__default['isClassDeclaration'](_0x2c9cde)||_0x2de5a4__default['isInterfaceDeclaration'](_0x2c9cde)||_0x2de5a4__default['isTypeAliasDeclaration'](_0x2c9cde)||_0x2de5a4__default['isEnumDeclaration'](_0x2c9cde)){const _0x292417=_0x2c9cde['name']?.['text'];_0x276771=_0x292417===_0x373f9d;}}return _0x276771;}__name(isNamedExportStatement,'isNamedExportStatement');function isNodeExported(_0x2b8d3a){return _0x2b8d3a?.['some'](_0x562a6e=>_0x562a6e['kind']===_0x2de5a4__default['SyntaxKind']['ExportKeyword'])??![];}__name(isNodeExported,'isNodeExported');function isNodeDefaultExport(_0x51054b){return _0x51054b?.['some'](_0xcdcea3=>_0xcdcea3['kind']===_0x2de5a4__default['SyntaxKind']['DefaultKeyword'])??![];}__name(isNodeDefaultExport,'isNodeDefaultExport');function getNodeModifiers(_0x144c0a){return _0x2de5a4__default['canHaveModifiers'](_0x144c0a)?_0x2de5a4__default['getModifiers'](_0x144c0a):void(-0x2*0xdb4+0x2cf*0x2+0x2*0xae5);}__name(getNodeModifiers,'getNodeModifiers');function getImportedSpecifierName(_0x1e7ba3){return _0x1e7ba3['imported']['type']===nodeTypes['identifier']?_0x1e7ba3['imported']['name']:_0x1e7ba3['imported']['value'];}__name(getImportedSpecifierName,'getImportedSpecifierName');function resolveImportFilePath(_0x49d31d,_0x256757){const _0x1f5af5=/^@([^/]+)\/(.+)$/,_0x48763b=normalizePathSeparators(_0x49d31d)['split']('?')['at'](-0x2089+-0x16*0x117+0x3883)??'',_0x2fc4fa=_0x1f5af5['exec'](_0x48763b);if(!_0x2fc4fa)return void(-0x1eaa+0x21cd+0x49*-0xb);const _0x224312=_0x2fc4fa[0x1918+-0xa73*0x1+-0x1*0xea4],_0x34bc5e=_0x2fc4fa[-0xebf+-0x610+0x49*0x49],_0x29d2cc=getRepoRoot(_0x256757),_0x450d33=[_0x1c3e3a['join'](_0x29d2cc,folders['packages'],_0x224312),_0x1c3e3a['join'](_0x29d2cc,folders['packages'],folders['level2'],_0x224312)];return resolveFirstExistingSourcePath(_0x450d33,_0x34bc5e);}__name(resolveImportFilePath,'resolveImportFilePath');function getRepoRoot(_0x2f704f){const _0x169b98=normalizePathSeparators(_0x1c3e3a['resolve'](_0x2f704f)),_0x35770c=_0x169b98['lastIndexOf'](packagesMarkerAbsolute);return _0x35770c>=-0xe80+0x10b7*0x1+-0x237?_0x169b98['slice'](0x3*-0x9ef+-0x1689+0x3456,_0x35770c):process['cwd']();}__name(getRepoRoot,'getRepoRoot');function resolveFirstExistingSourcePath(_0x110904,_0x52a846){for(const _0x4b6ec1 of _0x110904){const _0x13387c=normalizePathSeparators(_0x4b6ec1),_0x4ac714=_0x1c3e3a['join'](_0x13387c,_0x52a846),_0x5cf3f0=[...fileExtentions['map'](_0x5722cf=>''+_0x4ac714+_0x5722cf),...fileExtentions['map'](_0x548fdf=>_0x1c3e3a['join'](_0x4ac714,'index'+_0x548fdf))],_0x1625ef=_0x5cf3f0['find'](existsSync);if(_0x1625ef)return _0x1625ef;}return void(0x23*0xff+0xe*0x10f+-0x31af);}__name(resolveFirstExistingSourcePath,'resolveFirstExistingSourcePath');function isInternalNameSpecifier(_0x3fdd29){if(_0x3fdd29['type']===nodeTypes['importSpecifier']){const _0x10470b=_0x3fdd29['imported']['type']===nodeTypes['identifier']?_0x3fdd29['imported']['name']:_0x3fdd29['imported']['value'];return _0x10470b['startsWith'](folders['internal']);}return _0x3fdd29['local']['name']['startsWith'](folders['internal']);}__name(isInternalNameSpecifier,'isInternalNameSpecifier');function hasInternalPrefixInSourceFileName(_0x247be3){const _0x508768=normalizePathSeparators(_0x247be3)['split']('?')['at'](0x8*0x425+-0x2386*0x1+0x25e)??'';return _0x508768['split']('/')['filter'](Boolean)['map'](_0x574c71=>_0x574c71['replace'](/^@/,''))['some'](_0x311867=>_0x311867['startsWith'](folders['internal']));}__name(hasInternalPrefixInSourceFileName,'hasInternalPrefixInSourceFileName');function shouldCheckImportPath(_0xb41c3c,_0x5d5bd9){const _0x59fbc4=getNxProjectNameFromFilePath(_0x5d5bd9),_0x68eed6=getNxProjectNameFromImportPath(_0xb41c3c);if(!_0x59fbc4||!_0x68eed6)return ![];return _0x59fbc4!==_0x68eed6;}__name(shouldCheckImportPath,'shouldCheckImportPath');function getNxProjectNameFromFilePath(_0x5425c5){const _0x41febd=normalizePathSeparators(_0x5425c5),_0x17fb37=_0x41febd['lastIndexOf'](packagesMarkerAbsolute),_0x434eb3=_0x41febd['indexOf'](packagesMarkerRelative),_0x1870d8=_0x17fb37>=-0x2531+0x473+0x20be,_0x3d3cad=_0x434eb3>=-0x22c*-0x10+-0x1*-0x16e8+0xb88*-0x5,_0xfd051c=_0x1870d8||_0x3d3cad;let _0x421587='';if(_0x1870d8)_0x421587=_0x41febd['slice'](_0x17fb37+packagesMarkerAbsolute['length']);else _0x3d3cad&&(_0x421587=_0x41febd['slice'](_0x434eb3+packagesMarkerRelative['length']));const _0x1fe2d3=_0x421587['split']('/')['filter'](Boolean),_0x5ec6ac=_0x1fe2d3['length']>=0xa*0x61+-0xa6e+0x6a6,_0x3dcc38=_0x1fe2d3['at'](0x1*0x26ad+0xdc9+-0x3476),_0x1a3c5e=_0x3dcc38===folders['level2'];let _0x117e3e;return _0xfd051c&&_0x5ec6ac&&(_0x117e3e=_0x1a3c5e?_0x1fe2d3['at'](0x1*0x47b+-0x1*0x2176+0x1cfc):_0x3dcc38),_0x117e3e;}__name(getNxProjectNameFromFilePath,'getNxProjectNameFromFilePath');function getNxProjectNameFromImportPath(_0x2e38cf){const _0x46a83f=/^@([^/]+)\//,_0x2a70ab=_0x2e38cf['startsWith'](aliases['src']+'/'),_0x139162=_0x46a83f['exec'](_0x2e38cf),_0xdebc0f=_0x139162?.['at'](0x1712+0x5ae+-0x1cbf),_0x3548ca=!!_0xdebc0f&&nxAliasProjectNames['includes'](_0xdebc0f),_0x29986d=!_0x2a70ab&&_0x3548ca?_0xdebc0f:void(0x1221+-0x41b*0x1+-0xe06);return _0x29986d;}__name(getNxProjectNameFromImportPath,'getNxProjectNameFromImportPath');function getImportedSymbol(_0x3ea9c2,_0x50f9cb,_0x46bd8f){const _0x34274f=_0x50f9cb?_0x50f9cb['get'](_0x3ea9c2['local']):void(-0x41*0x11+-0x9a5+0xdf6),_0x1e8769=_0x34274f?_0x46bd8f['getSymbolAtLocation'](_0x34274f):void(0x57*-0x1d+-0x1d0b+0x1a*0x17f);return _0x1e8769;}__name(getImportedSymbol,'getImportedSymbol');

export { mustMarkExternalImportsAsInternal as default, ruleName };
