import*as CssOverviewModule from"../css_overview/css_overview.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const computedArrayFontSizeIndex=6;function getPxMultiplier(){return 1}async function getEmMultiplier(e){const t=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);let i;if(t&&t.parentNode&&"HTML"!==t.nodeName()){const[n]=SDK.SDKModel.TargetManager.instance().models(CssOverviewModule.CSSOverviewModel.CSSOverviewModel),o=e?t.parentNode.id:t.id,r=(await n.getComputedStyleForNode(o).then(findFontSizeValue)).replace(/[a-z]/g,"");i=parseFloat(r)}else i=16;return i}async function getRemMultiplier(){const e=findHtmlNode(UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode));if(!e||!e.id)return 16;const[t]=SDK.SDKModel.TargetManager.instance().models(CssOverviewModule.CSSOverviewModel.CSSOverviewModel),i=(await t.getComputedStyleForNode(e.id).then(findFontSizeValue)).replace(/[a-z]/g,"");return parseFloat(i)}async function getPercentMultiplier(e){return await getEmMultiplier(e)/100}async function getVhMultiplier(){const e=await getViewportObject();if(!e)return 1;return e.height/100}async function getVwMultiplier(){const e=await getViewportObject();if(!e)return 1;return e.width/100}async function getVminMultiplier(){const e=await getViewportObject();if(!e)return 1;const t=e.width,i=e.height;return Math.min(t,i)/100}async function getVmaxMultiplier(){const e=await getViewportObject();if(!e)return 1;const t=e.width,i=e.height;return Math.max(t,i)/100}function getCmMultiplier(){return 37.795}function getMmMultiplier(){return 3.7795}function getInMultiplier(){return 96}function getPtMultiplier(){return 4/3}function getPcMultiplier(){return 16}function findFontSizeValue(e){const t=e.computedStyle;let i=6;if(t[i].name&&"font-size"!==t[i].name)for(let n=0;n<t.length;n++)if("font-size"===t[n].name){i=n;break}return t[i].value}function findHtmlNode(e){let t=e;for(;t&&"HTML"!==t.nodeName()&&t.parentNode;)t=t.parentNode;return t}const widthEvaluateParams={expression:"window.innerWidth",objectGroup:void 0,includeCommandLineAPI:!1,silent:!0,contextId:void 0,returnByValue:!1,generatePreview:!1,userGesture:!1,awaitPromise:!0,throwOnSideEffect:!1,timeout:void 0,disableBreaks:!0,replMode:!1,allowUnsafeEvalBlockedByCSP:!1},heightEvaluateParams={expression:"window.innerHeight",objectGroup:void 0,includeCommandLineAPI:!1,silent:!0,contextId:void 0,returnByValue:!1,generatePreview:!1,userGesture:!1,awaitPromise:!0,throwOnSideEffect:!1,timeout:void 0,disableBreaks:!0,replMode:!1,allowUnsafeEvalBlockedByCSP:!1};async function getViewportObject(){const e=UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext);let t,i;if(e){const n=await e.evaluate(widthEvaluateParams,!1,!1),o=await e.evaluate(heightEvaluateParams,!1,!1);if("error"in n||"error"in o)return null;n.object&&(t=n.object.value),o.object&&(i=o.object.value)}if(void 0===t||void 0===i){const e=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);if(!e)return null;const n=await e.domModel().target().pageAgent().invoke_getLayoutMetrics(),o=n.visualViewport.zoom?n.visualViewport.zoom:1;i=n.visualViewport.clientHeight/o,t=n.visualViewport.clientWidth/o}return{width:t,height:i}}const unitConversionMap=new Map;unitConversionMap.set("px",getPxMultiplier),unitConversionMap.set("em",getEmMultiplier),unitConversionMap.set("rem",getRemMultiplier),unitConversionMap.set("%",getPercentMultiplier),unitConversionMap.set("vh",getVhMultiplier),unitConversionMap.set("vw",getVwMultiplier),unitConversionMap.set("vmin",getVminMultiplier),unitConversionMap.set("vmax",getVmaxMultiplier),unitConversionMap.set("cm",getCmMultiplier),unitConversionMap.set("mm",getMmMultiplier),unitConversionMap.set("in",getInMultiplier),unitConversionMap.set("pt",getPtMultiplier),unitConversionMap.set("pc",getPcMultiplier);export async function getUnitConversionMultiplier(e,t,i){let n,o;""===e&&(e="em"),""===t&&(t="em");const r=unitConversionMap.get(e),a=unitConversionMap.get(t);return r&&a?(n="em"===e||"%"===e?await r(i):await r(),o="em"===t||"%"===t?await a(i):await a(),n/o):1}