import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";const UIStrings={deprecatedSyntaxFoundPleaseUse:"Deprecated syntax found. Please use: <name>;dur=<duration>;desc=<description>",duplicateParameterSIgnored:'Duplicate parameter "{PH1}" ignored.',noValueFoundForParameterS:'No value found for parameter "{PH1}".',unrecognizedParameterS:'Unrecognized parameter "{PH1}".',extraneousTrailingCharacters:"Extraneous trailing characters.",unableToParseSValueS:'Unable to parse "{PH1}" value "{PH2}".',servertimingS:"ServerTiming: {PH1}"},str_=i18n.i18n.registerUIStrings("sdk/ServerTiming.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ServerTiming{constructor(r,e,n){this.metric=r,this.value=e,this.description=n}static parseHeaders(r){const e=r.filter((r=>"server-timing"===r.name.toLowerCase()));if(!e.length)return null;const n=e.reduce(((r,e)=>{const n=this.createFromHeaderValue(e.value);return r.push(...n.map((function(r){return new ServerTiming(r.name,r.hasOwnProperty("dur")?r.dur:null,r.hasOwnProperty("desc")?r.desc:"")}))),r}),[]);return n.sort(((r,e)=>Platform.StringUtilities.compare(r.metric.toLowerCase(),e.metric.toLowerCase()))),n}static createFromHeaderValue(r){function e(){r=r.replace(/^\s*/,"")}function n(n){return console.assert(1===n.length),e(),r.charAt(0)===n&&(r=r.substring(1),!0)}function t(){const e=/^(?:\s*)([\w!#$%&'*+\-.^`|~]+)(?:\s*)(.*)/.exec(r);return e?(r=e[2],e[1]):null}function i(){return e(),'"'===r.charAt(0)?function(){console.assert('"'===r.charAt(0)),r=r.substring(1);let e="";for(;r.length;){const n=/^([^"\\]*)(.*)/.exec(r);if(!n)return null;if(e+=n[1],'"'===n[2].charAt(0))return r=n[2].substring(1),e;console.assert("\\"===n[2].charAt(0)),e+=n[2].charAt(1),r=n[2].substring(2)}return null}():t()}function s(){const e=/([,;].*)/.exec(r);e&&(r=e[1])}const a=[];let o;for(;null!==(o=t());){const e={name:o};for("="===r.charAt(0)&&this.showWarning(i18nString(UIStrings.deprecatedSyntaxFoundPleaseUse));n(";");){let r;if(null===(r=t()))continue;r=r.toLowerCase();const a=this.getParserForParameter(r);let o=null;if(n("=")&&(o=i(),s()),a){if(e.hasOwnProperty(r)){this.showWarning(i18nString(UIStrings.duplicateParameterSIgnored,{PH1:r}));continue}null===o&&this.showWarning(i18nString(UIStrings.noValueFoundForParameterS,{PH1:r})),a.call(this,e,o)}else this.showWarning(i18nString(UIStrings.unrecognizedParameterS,{PH1:r}))}if(a.push(e),!n(","))break}return r.length&&this.showWarning(i18nString(UIStrings.extraneousTrailingCharacters)),a}static getParserForParameter(r){switch(r){case"dur":{function e(e,n){if(e.dur=0,null!==n){const t=parseFloat(n);if(isNaN(t))return void ServerTiming.showWarning(i18nString(UIStrings.unableToParseSValueS,{PH1:r,PH2:n}));e.dur=t}}return e}case"desc":{function r(r,e){r.desc=e||""}return r}default:return null}}static showWarning(r){Common.Console.Console.instance().warn(i18nString(UIStrings.servertimingS,{PH1:r}))}}