"use strict";var e=this&&this.__awaiter||function(e,t,s,i){return new(s||(s=Promise))((function(o,n){function r(e){try{c(i.next(e))}catch(e){n(e)}}function a(e){try{c(i.throw(e))}catch(e){n(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(r,a)}c((i=i.apply(e,t||[])).next())}))},t=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.DepStatementsMissingDetector=void 0;const s=t(require("path")),i=t(require("typescript")),o=require("module"),n=t(require("../../log")),r=require("../../common/Constants"),a=require("../../util/FsUtil"),c=require("../../common/message"),l=t(require("./ignoreImports.json")),u=require("glob");exports.DepStatementsMissingDetector=class{constructor(){this._missingMessages=[],this.ohPkgJsonCache=new Map,this.tsHost=i.default.createCompilerHost({allowJs:!0,noEmit:!0,isolatedModules:!0,resolveJsonModule:!1,moduleResolution:i.default.ModuleResolutionKind.Classic,incremental:!0,noLib:!0,noResolve:!0},!0)}detectMissingDeps(t){return e(this,void 0,void 0,(function*(){const e=new Set,s=(0,u.globSync)(["**/*.ets","**/*.ts","**/*.js"],{absolute:!0,ignore:["oh_modules/**","test/**","hvigorfile.ts","BuildProfile.ts"],cwd:t}),i=[];for(const o of s)try{i.push(this.missingDetect(t,o,e))}catch(e){throw n.default.debug("",`error occurred when detect source code file: "${o}", error: ${e}`),e}return yield Promise.all(i),Array.from(e.values())}))}get missingMessages(){return this._missingMessages}isOhSdk(e){for(const t of l.default.arkTsApiPrefixList)if(e.startsWith(t))return!0;return!1}missingDetect(t,s,i){return e(this,void 0,void 0,(function*(){const e=this.getImportModules(s),o=yield this.fetchDepStatements(s,t),r=a.FsUtil.slash(t),c=a.FsUtil.slash(s).replace(r,"package/");n.default.debug("",`Detecting source file: ${c}`);for(const t of e){let e;e=t.startsWith("@")?t.split("/").slice(0,2).join("/"):t.split("/")[0],o[e]||(this._missingMessages.push(`Missing dependency statement "${e}" in "dependencies" or "dynamicDependencies" from "oh-package.json5", which imported by "${c}"`),i.add(e))}}))}getImportModules(e){const t=this.tsHost.getSourceFile(e,i.default.ScriptTarget.Latest,(t=>{throw new Error(`Failed to parse ${e}: ${t}`)}));if(!t)throw ReferenceError(`Failed to find file ${e}`);const s=[],n=e=>{if(i.default.isImportDeclaration(e)){const t=e.moduleSpecifier.getText().replace(/['"]/g,"");o.builtinModules.includes(t)||l.default.ignoreImports.includes(t)||this.isLocalModule(t)||this.isOhSdk(t)||t.endsWith(".so")||s.push(t)}else i.default.forEachChild(e,n)};return n(t),s}fetchDepStatements(t,i){return e(this,void 0,void 0,(function*(){const e=s.default.dirname(t);let o=this.ohPkgJsonCache.get(e);if(!o){const t=yield this.findOhPackageJsonFile(e,i);o=yield a.FsUtil.readJSON5(t),this.ohPkgJsonCache.set(e,o)}return Object.assign(Object.assign({},o.dependencies),o.dynamicDependencies)}))}findOhPackageJsonFile(t,i){return e(this,void 0,void 0,(function*(){let e=t;for(;e!==i;e=s.default.resolve(e,".."))if(yield this.checkPkgDir(e))return s.default.join(e,r.Constants.MyPackageJson);if(yield this.checkPkgDir(e))return s.default.join(e,r.Constants.MyPackageJson);throw Error((0,c.format)(c.Messages.Common.PkgNotFound))}))}checkPkgDir(t){return e(this,void 0,void 0,(function*(){return a.FsUtil.exists(s.default.join(t,r.Constants.MyPackageJson))}))}isLocalModule(e){return e.startsWith("@/")||e.startsWith(".")}};