import*as Platform from"../platform/platform.js";import*as Root from"../root/root.js";export class ParsedURL{constructor(t){this.isValid=!1,this.url=t,this.scheme="",this.user="",this.host="",this.port="",this.path="",this.queryParams="",this.fragment="",this.folderPathComponents="",this.lastPathComponent="";const s=this.url.startsWith("blob:"),r=(s?t.substring(5):t).match(ParsedURL._urlRegex());if(r)this.isValid=!0,s?(this._blobInnerScheme=r[2].toLowerCase(),this.scheme="blob"):this.scheme=r[2].toLowerCase(),this.user=r[3],this.host=r[4],this.port=r[5],this.path=r[6]||"/",this.queryParams=r[7]||"",this.fragment=r[8];else{if(this.url.startsWith("data:"))return void(this.scheme="data");if(this.url.startsWith("blob:"))return void(this.scheme="blob");if("about:blank"===this.url)return void(this.scheme="about");this.path=this.url}const e=this.path.lastIndexOf("/");-1!==e?(this.folderPathComponents=this.path.substring(0,e),this.lastPathComponent=this.path.substring(e+1)):this.lastPathComponent=this.path}static fromString(t){const s=new ParsedURL(t.toString());return s.isValid?s:null}static platformPathToURL(t){return(t=t.replace(/\\/g,"/")).startsWith("file://")||(t=t.startsWith("/")?"file://"+t:"file:///"+t),t}static urlToPlatformPath(t,s){return console.assert(t.startsWith("file://"),"This must be a file URL."),s?t.substr("file:///".length).replace(/\//g,"\\"):t.substr("file://".length)}static urlWithoutHash(t){const s=t.indexOf("#");return-1!==s?t.substr(0,s):t}static _urlRegex(){if(ParsedURL._urlRegexInstance)return ParsedURL._urlRegexInstance;return ParsedURL._urlRegexInstance=new RegExp("^("+/([A-Za-z][A-Za-z0-9+.-]*):\/\//.source+/(?:([A-Za-z0-9\-._~%!$&'()*+,;=:]*)@)?/.source+/((?:\[::\d?\])|(?:[^\s\/:]*))/.source+/(?::([\d]+))?/.source+")"+/(\/[^#?]*)?/.source+/(?:\?([^#]*))?/.source+/(?:#(.*))?/.source+"$"),ParsedURL._urlRegexInstance}static extractPath(t){const s=this.fromString(t);return s?s.path:""}static extractOrigin(t){const s=this.fromString(t);return s?s.securityOrigin():""}static extractExtension(t){const s=(t=ParsedURL.urlWithoutHash(t)).indexOf("?");-1!==s&&(t=t.substr(0,s));const r=t.lastIndexOf("/");-1!==r&&(t=t.substr(r+1));const e=t.lastIndexOf(".");if(-1!==e){const s=(t=t.substr(e+1)).indexOf("%");return-1!==s?t.substr(0,s):t}return""}static extractName(t){let s=t.lastIndexOf("/");const r=-1!==s?t.substr(s+1):t;return s=r.indexOf("?"),s<0?r:r.substr(0,s)}static completeURL(t,s){const r=s.trim();if(r.startsWith("data:")||r.startsWith("blob:")||r.startsWith("javascript:")||r.startsWith("mailto:"))return s;const e=this.fromString(r);if(e&&e.scheme)return r;const i=this.fromString(t);if(!i)return null;if(i.isDataURL())return s;if(s.length>1&&"/"===s.charAt(0)&&"/"===s.charAt(1))return i.scheme+":"+s;const a=i.securityOrigin(),h=i.path,n=i.queryParams?"?"+i.queryParams:"";if(!s.length)return a+h+n;if("#"===s.charAt(0))return a+h+n+s;if("?"===s.charAt(0))return a+h+s;const o=s.match(/^[^#?]*/);if(!o||!s.length)throw new Error("Invalid href");let l=o[0];const u=s.substring(l.length);return"/"!==l.charAt(0)&&(l=i.folderPathComponents+"/"+l),a+Root.Runtime.Runtime.normalizePath(l)+u}static splitLineAndColumn(t){const s=t.match(ParsedURL._urlRegex());let r="",e=t;s&&(r=s[1],e=t.substring(s[1].length));const i=/(?::(\d+))?(?::(\d+))?$/.exec(e);let a,h;return console.assert(Boolean(i)),i?("string"===typeof i[1]&&(a=parseInt(i[1],10),a=isNaN(a)?void 0:a-1),"string"===typeof i[2]&&(h=parseInt(i[2],10),h=isNaN(h)?void 0:h-1),{url:r+e.substring(0,e.length-i[0].length),lineNumber:a,columnNumber:h}):{url:t,lineNumber:0,columnNumber:0}}static removeWasmFunctionInfoFromURL(t){const s=t.search(/:wasm-function\[\d+\]/);return-1===s?t:t.substring(0,s)}static isRelativeURL(t){return!/^[A-Za-z][A-Za-z0-9+.-]*:/.test(t)}get displayName(){return this._displayName?this._displayName:this.isDataURL()?this.dataURLDisplayName():this.isBlobURL()||this.isAboutBlank()?this.url:(this._displayName=this.lastPathComponent,this._displayName||(this._displayName=(this.host||"")+"/"),"/"===this._displayName&&(this._displayName=this.url),this._displayName)}dataURLDisplayName(){return this._dataURLDisplayName?this._dataURLDisplayName:this.isDataURL()?(this._dataURLDisplayName=Platform.StringUtilities.trimEndWithMaxLength(this.url,20),this._dataURLDisplayName):""}isAboutBlank(){return"about:blank"===this.url}isDataURL(){return"data"===this.scheme}isHttpOrHttps(){return"http"===this.scheme||"https"===this.scheme}isBlobURL(){return this.url.startsWith("blob:")}lastPathComponentWithFragment(){return this.lastPathComponent+(this.fragment?"#"+this.fragment:"")}domain(){return this.isDataURL()?"data:":this.host+(this.port?":"+this.port:"")}securityOrigin(){if(this.isDataURL())return"data:";return(this.isBlobURL()?this._blobInnerScheme:this.scheme)+"://"+this.domain()}urlWithoutScheme(){return this.scheme&&this.url.startsWith(this.scheme+"://")?this.url.substring(this.scheme.length+3):this.url}}ParsedURL._urlRegexInstance=null;