import{TokenNode as e,isTokenNode as n,isCommentNode as t,isWhitespaceNode as r,isSimpleBlockNode as u,isFunctionNode as i,FunctionNode as a,WhitespaceNode as o,parseCommaSeparatedListOfComponentValues as l,replaceComponentValues as c}from"@csstools/css-parser-algorithms";import{TokenType as s,NumberType as v,isToken as m,tokenizer as f,tokenize as p,stringify as N}from"@csstools/css-tokenizer";const g=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(g,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const b={cm:"px",in:"px",mm:"px",pc:"px",pt:"px",px:"px",q:"px",deg:"deg",grad:"deg",rad:"deg",turn:"deg",ms:"s",s:"s",hz:"hz",khz:"hz"},d=new Map([["cm",e=>e],["mm",e=>10*e],["q",e=>40*e],["in",e=>e/2.54],["pc",e=>e/2.54*6],["pt",e=>e/2.54*72],["px",e=>e/2.54*96]]),w=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),h=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),C=new Map([["hz",e=>e],["khz",e=>e/1e3]]),I=new Map([["cm",e=>2.54*e],["mm",e=>25.4*e],["q",e=>25.4*e*4],["in",e=>e],["pc",e=>6*e],["pt",e=>72*e],["px",e=>96*e]]),S=new Map([["hz",e=>1e3*e],["khz",e=>e]]),y=new Map([["cm",e=>e/10],["mm",e=>e],["q",e=>4*e],["in",e=>e/25.4],["pc",e=>e/25.4*6],["pt",e=>e/25.4*72],["px",e=>e/25.4*96]]),M=new Map([["ms",e=>e],["s",e=>e/1e3]]),P=new Map([["cm",e=>e/6*2.54],["mm",e=>e/6*25.4],["q",e=>e/6*25.4*4],["in",e=>e/6],["pc",e=>e],["pt",e=>e/6*72],["px",e=>e/6*96]]),D=new Map([["cm",e=>e/72*2.54],["mm",e=>e/72*25.4],["q",e=>e/72*25.4*4],["in",e=>e/72],["pc",e=>e/72*6],["pt",e=>e],["px",e=>e/72*96]]),T=new Map([["cm",e=>e/96*2.54],["mm",e=>e/96*25.4],["q",e=>e/96*25.4*4],["in",e=>e/96],["pc",e=>e/96*6],["pt",e=>e/96*72],["px",e=>e]]),A=new Map([["cm",e=>e/4/10],["mm",e=>e/4],["q",e=>e],["in",e=>e/4/25.4],["pc",e=>e/4/25.4*6],["pt",e=>e/4/25.4*72],["px",e=>e/4/25.4*96]]),k=new Map([["deg",e=>180*e/Math.PI],["grad",e=>180*e/Math.PI/.9],["rad",e=>e],["turn",e=>180*e/Math.PI/360]]),F=new Map([["ms",e=>1e3*e],["s",e=>e]]),x=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),O=new Map([["cm",d],["mm",y],["q",A],["in",I],["pc",P],["pt",D],["px",T],["ms",M],["s",F],["deg",w],["grad",h],["rad",k],["turn",x],["hz",C],["khz",S]]);function convertUnit(e,n){if(e[0]!==s.Dimension)return n;if(n[0]!==s.Dimension)return n;const t=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(n[4].unit);if(t===r)return n;const u=O.get(r);if(!u)return n;const i=u.get(t);if(!i)return n;const a=i(n[4].value);return[s.Dimension,a.toString()+e[4].unit,n[2],n[3],{value:a,unit:e[4].unit,type:Number.isInteger(a)?v.Integer:v.Number}]}function toCanonicalUnit(e){if(e[0]!==s.Dimension)return e;const n=toLowerCaseAZ(e[4].unit),t=b[n];if(n===t)return e;const r=O.get(n);if(!r)return e;const u=r.get(t);if(!u)return e;const i=u(e[4].value);return[s.Dimension,i.toString()+t,e[2],e[3],{value:i,unit:t,type:Number.isInteger(i)?v.Integer:v.Number}]}function addition(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(t[0]===s.Number&&r[0]===s.Number){const n=t[4].value+r[4].value;return new e([s.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===v.Integer&&r[4].type===v.Integer?v.Integer:v.Number}])}if(t[0]===s.Percentage&&r[0]===s.Percentage){const n=t[4].value+r[4].value;return new e([s.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(t[0]===s.Dimension&&r[0]===s.Dimension&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value+r[4].value;return new e([s.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===v.Integer&&r[4].type===v.Integer?v.Integer:v.Number,unit:t[4].unit}])}return-1}function division(n){if(2!==n.length)return-1;const t=n[0].value,r=n[1].value;if(t[0]===s.Number&&r[0]===s.Number){const n=t[4].value/r[4].value;return new e([s.Number,n.toString(),t[2],r[3],{value:n,type:Number.isInteger(n)?v.Integer:v.Number}])}if(t[0]===s.Percentage&&r[0]===s.Number){const n=t[4].value/r[4].value;return new e([s.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(t[0]===s.Dimension&&r[0]===s.Number){const n=t[4].value/r[4].value;return new e([s.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:Number.isInteger(n)?v.Integer:v.Number,unit:t[4].unit}])}return-1}function isCalculation(e){return!!e&&"object"==typeof e&&"inputs"in e&&Array.isArray(e.inputs)&&"operation"in e}function solve(e){if(-1===e)return-1;const t=[];for(let r=0;re.type))).size)return-1;const u=r.value;if(!isNumeric(u))return-1;const i=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(i))return-1;const a=i.map((e=>e[4].value)),o=Math.hypot(...a);return resultToCalculation(e,u,o)}function solveMax(e,t){const r=t[0];if(!r||!n(r))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const u=r.value;if(!isNumeric(u))return-1;const i=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(i))return-1;const a=i.map((e=>e[4].value)),o=Math.max(...a);return resultToCalculation(e,u,o)}function solveMin(e,t){const r=t[0];if(!r||!n(r))return-1;if(1!==new Set(t.map((e=>e.type))).size)return-1;const u=r.value;if(!isNumeric(u))return-1;const i=t.map((e=>convertUnit(u,e.value)));if(!arrayOfSameNumeric(i))return-1;const a=i.map((e=>e[4].value)),o=Math.min(...a);return resultToCalculation(e,u,o)}function solveMod(e,n,t){const r=n.value;if(!isNumeric(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let i;return i=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)&&(Number.isFinite(u[4].value)||(u[4].value!==Number.POSITIVE_INFINITY||r[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*r[4].value,-0))&&(u[4].value!==Number.NEGATIVE_INFINITY||r[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*r[4].value,0)))?Number.isFinite(u[4].value)?(r[4].value%u[4].value+u[4].value)%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,i)}function solvePow(e,n,t){const r=n.value,u=t.value;if(r[0]!==s.Number)return-1;if(!twoOfSameNumeric(r,u))return-1;return numberToCalculation(e,Math.pow(r[4].value,u[4].value))}function solveRem(e,n,t){const r=n.value;if(!isNumeric(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let i;return i=0===u[4].value?Number.NaN:Number.isFinite(r[4].value)?Number.isFinite(u[4].value)?r[4].value%u[4].value:r[4].value:Number.NaN,resultToCalculation(e,r,i)}function solveRound(e,n,t,r){const u=t.value;if(!isNumeric(u))return-1;const i=convertUnit(u,r.value);if(!twoOfSameNumeric(u,i))return-1;let a;if(0===i[4].value)a=Number.NaN;else if(Number.isFinite(u[4].value)||Number.isFinite(i[4].value))if(!Number.isFinite(u[4].value)&&Number.isFinite(i[4].value))a=u[4].value;else if(Number.isFinite(u[4].value)&&!Number.isFinite(i[4].value))switch(n){case"down":a=u[4].value<0?-1/0:Object.is(-0,0*u[4].value)?-0:0;break;case"up":a=u[4].value>0?1/0:Object.is(0,0*u[4].value)?0:-0;break;default:a=Object.is(0,0*u[4].value)?0:-0}else if(Number.isFinite(i[4].value))switch(n){case"down":a=Math.floor(u[4].value/i[4].value)*i[4].value;break;case"up":a=Math.ceil(u[4].value/i[4].value)*i[4].value;break;case"to-zero":a=Math.trunc(u[4].value/i[4].value)*i[4].value;break;default:{let e=Math.floor(u[4].value/i[4].value)*i[4].value,n=Math.ceil(u[4].value/i[4].value)*i[4].value;if(e>n){const t=e;e=n,n=t}const t=Math.abs(u[4].value-e),r=Math.abs(u[4].value-n);a=t===r?n:t0?1/0:-1/0:Math.tan(i),numberToCalculation(e,i)}function subtraction(n){if(2!==n.length)return-1;const t=n[0].value;let r=n[1].value;if(t[0]===s.Number&&r[0]===s.Number){const n=t[4].value-r[4].value;return new e([s.Number,n.toString(),t[2],r[3],{value:n,type:t[4].type===v.Integer&&r[4].type===v.Integer?v.Integer:v.Number}])}if(t[0]===s.Percentage&&r[0]===s.Percentage){const n=t[4].value-r[4].value;return new e([s.Percentage,n.toString()+"%",t[2],r[3],{value:n}])}if(t[0]===s.Dimension&&r[0]===s.Dimension&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const n=t[4].value-r[4].value;return new e([s.Dimension,n.toString()+t[4].unit,t[2],r[3],{value:n,type:t[4].type===v.Integer&&r[4].type===v.Integer?v.Integer:v.Number,unit:t[4].unit}])}return-1}function solveLog(e,t){if(1===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(u[0]!==s.Number)return-1;return numberToCalculation(e,Math.log(u[4].value))}if(2===t.length){const r=t[0];if(!r||!n(r))return-1;const u=r.value;if(u[0]!==s.Number)return-1;const i=t[1];if(!i||!n(i))return-1;const a=i.value;if(a[0]!==s.Number)return-1;return numberToCalculation(e,Math.log(u[4].value)/Math.log(a[4].value))}return-1}const Z=new Map([["abs",function abs(e,n){return singleNodeSolver(e,n,solveAbs)}],["acos",function acos(e,n){return singleNodeSolver(e,n,solveACos)}],["asin",function asin(e,n){return singleNodeSolver(e,n,solveASin)}],["atan",function atan(e,n){return singleNodeSolver(e,n,solveATan)}],["atan2",function atan2(e,n){return twoCommaSeparatedNodesSolver(e,n,solveATan2)}],["calc",calc$1],["clamp",function clamp(e,u){const i=resolveGlobalsAndConstants([...e.value.filter((e=>!t(e)&&!r(e)))],u),o=[],l=[],c=[];{let e=o;for(let t=0;t!t(e)&&!r(e)))],u);let o="";const l=[],c=[];{let e=l;for(let t=0;t!t(e)&&!r(e)))],a);if(1===o.length&&n(o[0]))return{inputs:[o[0]],operation:unary};let l=0;for(;l!t(e)&&!r(e)))],n),o=solve(calc$1(new a([s.Function,"calc(",-1,-1,{value:"calc"}],[s.CloseParen,")",-1,-1,void 0],i),n));return-1===o?-1:u(e,o)}function twoCommaSeparatedNodesSolver(e,u,i){const o=resolveGlobalsAndConstants([...e.value.filter((e=>!t(e)&&!r(e)))],u),l=[],c=[];{let e=l;for(let t=0;t!t(e)&&!r(e)))],u),l=[];{const e=[];let t=[];for(let r=0;re.map((e=>N(...e.tokens()))).join(""))).join(",")}function calcFromComponentValues(e,n){const t=tokenizeGlobals(null==n?void 0:n.globals);return c(e,(e=>{if(i(e)){const r=Z.get(toLowerCaseAZ(e.getName()));if(r){const u=patchCalcResult(solve(r(e,t)),n);if(-1!==u)return u}}}))}const z=new Set(Z.keys());export{calc,calcFromComponentValues,z as mathFunctionNames};