"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ohpa=void 0;const e=require("./OhpaResult"),s=require("./OhpaType"),r=require("../../common/message"),t=require("../../util/FsUtil"),i=require("../../common/Constants"),a=require("../../core/dist-tags/isStandardTag"),n=require("../../core/dependency/util"),{WindowsUtil:o}=require("../../util/is-windows"),{validatePkg:l}=require("./ValidatePkg"),c=require("url"),p=require("semver"),d=o.isWindows?require("path").win32:require("path"),{homedir:h}=require("os");class g{parse(e,s){if(g.isURL.test(e)||g.isGit.test(e))throw new Error((0,r.format)(r.Messages.Ohpa.PkgInvalidError));let t="",i="";const a="@"===e[0]?e.slice(1).indexOf("@")+1:e.indexOf("@"),n=a>0?e.slice(0,a):e;if("@"!==n[0]&&a<0&&this.isLocalDependency(n))i=e;else{const s=l.validate(n);if(s)throw new Error(s);t=n,a>0&&(i=e.slice(a+1))}return this.resolve(t,i,s,e)}validSpec(e,s){if(e&&(0,n.isTagDependency)(e)){const s=e.slice(i.Constants.TAG_PREFIX.length);return(0,a.isStandardTag)(s)?e:""}if(e&&(this.isLocalDependency(e)||this.isInnerPkgDependency(e))){const r=((g.REGEX_LOCAL.test(e)?"":g.LOCAL_PREFIX)+e).replace(g.REGEX_LOCAL,"");return`file:${d.resolve(s,r)}`}return p.validRange(e)}resolve(s,r,t,i){const a=(new e.OhpaResult).setRaw(i).setRawSpec(r);return s&&(a.setName(s),a.setEscapedName(s.replace("/","%2f")),a.setScope("@"===s[0]?s.slice(0,s.indexOf("/")):"")),r&&!(0,n.isTagDependency)(r)&&(this.isLocalDependency(r)||this.isInnerPkgDependency(r))?this.fromFile(a,t):this.fromRegistry(a)}fromFile(e,r){r=r||process.cwd(),e.setWhere(r);const t=this.isLocalFile(e.getRawSpec())?s.OhpaType.File:s.OhpaType.SourceCode;e.setType(t);const i=(g.REGEX_LOCAL.test(e.getRawSpec())?"":g.LOCAL_PREFIX)+e.getRawSpec(),a=i.replace(g.REGEX_LOCAL,""),n=this.convertToUrl(e,i,r),o=this.removeHeaderSepOnWin(n.specUrl,n.resolvedUrl),l=this.resolveSaveSpec(a,r,o);return e.setSaveSpec(l),e.setFetchSpec(d.resolve(r,o.resolvedPath)),e}fromRegistry(e){e.setRegistry(!0);const t=""===e.getRawSpec()?g.LATEST:e.getRawSpec().trim();e.setFetchSpec(t),e.setSaveSpec(t);const o=p.valid(t,!0),l=p.validRange(t,!0);if(o)e.setType(s.OhpaType.Version);else if(l)e.setType(s.OhpaType.Range);else{let o=t;if((0,n.isTagDependency)(t)&&(o=t.slice(i.Constants.TAG_PREFIX.length),!(0,a.isStandardTag)(o)))throw new Error((0,r.format)(r.Messages.DistTags.TagPkgInvalidError,{tag:o,pkg:e.getRaw()}));if(encodeURIComponent(o)!==o)throw new Error((0,r.format)(r.Messages.Ohpa.SpecUriError,{spec:t,raw:e.getRaw()}));e.setType(s.OhpaType.Tag)}return e}removeHeaderSepOnWin(e,s){let r=e.pathname,t=s.pathname;return o.isWindows&&(r=r.replace(/^\/+([a-z]:\/)/i,"$1"),t=t.replace(/^\/+([a-z]:\/)/i,"$1")),{specPath:r,resolvedPath:t}}convertToUrl(e,s,t){try{const e=new c.URL(s,`file://${d.resolve(t)}/`);return{resolvedUrl:e,specUrl:new c.URL(s)}}catch(s){throw new Error((0,r.format)(r.Messages.Ohpa.SpecInvalidError,{spec:e.getRawSpec(),error:s.message}))}}resolveSaveSpec(e,s,r){let i;return g.REGEX_PATH_LINUX.test(r.specPath)?(i=`file:${r.specPath.slice(1)}`,r.resolvedPath=d.resolve(h(),r.specPath.slice(3))):i=`file:${t.FsUtil.slash(d.relative(s,r.resolvedPath))}`,i}isLocalDependency(e){return g.REGEX_LOCAL.test(e)||g.hasSep.test(e)||g.isFile.test(e)||g.isDir.test(e)}isInnerPkgDependency(e){return g.LATEST!==e&&!p.validRange(e,{loose:!0,includePrerelease:!0})&&g.REGEX_START_EN.test(e)}isLocalFile(e){return g.isFile.test(e)}}g.REGEX_LOCAL=/^file:/i,g.hasSep=o.isWindows?/[\\/]/:/\//,g.isDir=o.isWindows?/^(?:[.]|~\/|[/\\]|[a-z]:)/i:/^(?:[.]|~\/|\/|[a-z]:)/i,g.isFile=/[.](?:tgz|tar.gz|tar|har)$/i,g.isURL=/^git[+][a-z]+:/i,g.isGit=/^[^@]+@[^:.]+\.[^:]+:.+$/i,g.REGEX_PATH_LINUX=/^\/~(\/|$)/,g.REGEX_START_EN=/^[a-z]/i,g.LOCAL_PREFIX="file:",g.LATEST="latest",exports.ohpa=new g;