"use strict";var e=require("@csstools/css-parser-algorithms"),n=require("@csstools/css-tokenizer");const o=/[A-Z]/g;function toLowerCaseAZ(e){return e.replace(o,(e=>String.fromCharCode(e.charCodeAt(0)+32)))}const t={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"},r=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]]),u=new Map([["deg",e=>e],["grad",e=>e/.9],["rad",e=>e/180*Math.PI],["turn",e=>e/360]]),i=new Map([["deg",e=>.9*e],["grad",e=>e],["rad",e=>.9*e/180*Math.PI],["turn",e=>.9*e/360]]),a=new Map([["hz",e=>e],["khz",e=>e/1e3]]),l=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]]),c=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]]),T=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]]),v=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]]),m=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]]),N=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]]),f=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]]),y=new Map([["ms",e=>1e3*e],["s",e=>e]]),k=new Map([["deg",e=>360*e],["grad",e=>360*e/.9],["rad",e=>360*e/180*Math.PI],["turn",e=>e]]),d=new Map([["cm",r],["mm",c],["q",N],["in",l],["pc",p],["pt",v],["px",m],["ms",T],["s",y],["deg",u],["grad",i],["rad",f],["turn",k],["hz",a],["khz",s]]);function convertUnit(e,o){if(e[0]!==n.TokenType.Dimension)return o;if(o[0]!==n.TokenType.Dimension)return o;const t=toLowerCaseAZ(e[4].unit),r=toLowerCaseAZ(o[4].unit);if(t===r)return o;const u=d.get(r);if(!u)return o;const i=u.get(t);if(!i)return o;const a=i(o[4].value);return[n.TokenType.Dimension,a.toString()+e[4].unit,o[2],o[3],{value:a,unit:e[4].unit,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number}]}function toCanonicalUnit(e){if(e[0]!==n.TokenType.Dimension)return e;const o=toLowerCaseAZ(e[4].unit),r=t[o];if(o===r)return e;const u=d.get(o);if(!u)return e;const i=u.get(r);if(!i)return e;const a=i(e[4].value);return[n.TokenType.Dimension,a.toString()+r,e[2],e[3],{value:a,unit:r,type:Number.isInteger(a)?n.NumberType.Integer:n.NumberType.Number}]}function addition(o){if(2!==o.length)return-1;const t=o[0].value;let r=o[1].value;if(t[0]===n.TokenType.Number&&r[0]===n.TokenType.Number){const o=t[4].value+r[4].value;return new e.TokenNode([n.TokenType.Number,o.toString(),t[2],r[3],{value:o,type:t[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(t[0]===n.TokenType.Percentage&&r[0]===n.TokenType.Percentage){const o=t[4].value+r[4].value;return new e.TokenNode([n.TokenType.Percentage,o.toString()+"%",t[2],r[3],{value:o}])}if(t[0]===n.TokenType.Dimension&&r[0]===n.TokenType.Dimension&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const o=t[4].value+r[4].value;return new e.TokenNode([n.TokenType.Dimension,o.toString()+t[4].unit,t[2],r[3],{value:o,type:t[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:t[4].unit}])}return-1}function division(o){if(2!==o.length)return-1;const t=o[0].value,r=o[1].value;if(t[0]===n.TokenType.Number&&r[0]===n.TokenType.Number){const o=t[4].value/r[4].value;return new e.TokenNode([n.TokenType.Number,o.toString(),t[2],r[3],{value:o,type:Number.isInteger(o)?n.NumberType.Integer:n.NumberType.Number}])}if(t[0]===n.TokenType.Percentage&&r[0]===n.TokenType.Number){const o=t[4].value/r[4].value;return new e.TokenNode([n.TokenType.Percentage,o.toString()+"%",t[2],r[3],{value:o}])}if(t[0]===n.TokenType.Dimension&&r[0]===n.TokenType.Number){const o=t[4].value/r[4].value;return new e.TokenNode([n.TokenType.Dimension,o.toString()+t[4].unit,t[2],r[3],{value:o,type:Number.isInteger(o)?n.NumberType.Integer:n.NumberType.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(n){if(-1===n)return-1;const o=[];for(let t=0;te.type))).size)return-1;const r=t.value;if(!isNumeric(r))return-1;const u=o.map((e=>convertUnit(r,e.value)));if(!arrayOfSameNumeric(u))return-1;const i=u.map((e=>e[4].value)),a=Math.hypot(...i);return resultToCalculation(n,r,a)}function solveMax(n,o){const t=o[0];if(!t||!e.isTokenNode(t))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const r=t.value;if(!isNumeric(r))return-1;const u=o.map((e=>convertUnit(r,e.value)));if(!arrayOfSameNumeric(u))return-1;const i=u.map((e=>e[4].value)),a=Math.max(...i);return resultToCalculation(n,r,a)}function solveMin(n,o){const t=o[0];if(!t||!e.isTokenNode(t))return-1;if(1!==new Set(o.map((e=>e.type))).size)return-1;const r=t.value;if(!isNumeric(r))return-1;const u=o.map((e=>convertUnit(r,e.value)));if(!arrayOfSameNumeric(u))return-1;const i=u.map((e=>e[4].value)),a=Math.min(...i);return resultToCalculation(n,r,a)}function solveMod(e,n,o){const t=n.value;if(!isNumeric(t))return-1;const r=convertUnit(t,o.value);if(!twoOfSameNumeric(t,r))return-1;let u;return u=0===r[4].value?Number.NaN:Number.isFinite(t[4].value)&&(Number.isFinite(r[4].value)||(r[4].value!==Number.POSITIVE_INFINITY||t[4].value!==Number.NEGATIVE_INFINITY&&!Object.is(0*t[4].value,-0))&&(r[4].value!==Number.NEGATIVE_INFINITY||t[4].value!==Number.POSITIVE_INFINITY&&!Object.is(0*t[4].value,0)))?Number.isFinite(r[4].value)?(t[4].value%r[4].value+r[4].value)%r[4].value:t[4].value:Number.NaN,resultToCalculation(e,t,u)}function solvePow(e,o,t){const r=o.value,u=t.value;if(r[0]!==n.TokenType.Number)return-1;if(!twoOfSameNumeric(r,u))return-1;return numberToCalculation(e,Math.pow(r[4].value,u[4].value))}function solveRem(e,n,o){const t=n.value;if(!isNumeric(t))return-1;const r=convertUnit(t,o.value);if(!twoOfSameNumeric(t,r))return-1;let u;return u=0===r[4].value?Number.NaN:Number.isFinite(t[4].value)?Number.isFinite(r[4].value)?t[4].value%r[4].value:t[4].value:Number.NaN,resultToCalculation(e,t,u)}function solveRound(e,n,o,t){const r=o.value;if(!isNumeric(r))return-1;const u=convertUnit(r,t.value);if(!twoOfSameNumeric(r,u))return-1;let i;if(0===u[4].value)i=Number.NaN;else if(Number.isFinite(r[4].value)||Number.isFinite(u[4].value))if(!Number.isFinite(r[4].value)&&Number.isFinite(u[4].value))i=r[4].value;else if(Number.isFinite(r[4].value)&&!Number.isFinite(u[4].value))switch(n){case"down":i=r[4].value<0?-1/0:Object.is(-0,0*r[4].value)?-0:0;break;case"up":i=r[4].value>0?1/0:Object.is(0,0*r[4].value)?0:-0;break;default:i=Object.is(0,0*r[4].value)?0:-0}else if(Number.isFinite(u[4].value))switch(n){case"down":i=Math.floor(r[4].value/u[4].value)*u[4].value;break;case"up":i=Math.ceil(r[4].value/u[4].value)*u[4].value;break;case"to-zero":i=Math.trunc(r[4].value/u[4].value)*u[4].value;break;default:{let e=Math.floor(r[4].value/u[4].value)*u[4].value,n=Math.ceil(r[4].value/u[4].value)*u[4].value;if(e>n){const o=e;e=n,n=o}const o=Math.abs(r[4].value-e),t=Math.abs(r[4].value-n);i=o===t?n:o0?1/0:-1/0:Math.tan(l),numberToCalculation(e,l)}function subtraction(o){if(2!==o.length)return-1;const t=o[0].value;let r=o[1].value;if(t[0]===n.TokenType.Number&&r[0]===n.TokenType.Number){const o=t[4].value-r[4].value;return new e.TokenNode([n.TokenType.Number,o.toString(),t[2],r[3],{value:o,type:t[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number}])}if(t[0]===n.TokenType.Percentage&&r[0]===n.TokenType.Percentage){const o=t[4].value-r[4].value;return new e.TokenNode([n.TokenType.Percentage,o.toString()+"%",t[2],r[3],{value:o}])}if(t[0]===n.TokenType.Dimension&&r[0]===n.TokenType.Dimension&&(r=convertUnit(t,r),toLowerCaseAZ(t[4].unit)===toLowerCaseAZ(r[4].unit))){const o=t[4].value-r[4].value;return new e.TokenNode([n.TokenType.Dimension,o.toString()+t[4].unit,t[2],r[3],{value:o,type:t[4].type===n.NumberType.Integer&&r[4].type===n.NumberType.Integer?n.NumberType.Integer:n.NumberType.Number,unit:t[4].unit}])}return-1}function solveLog(o,t){if(1===t.length){const r=t[0];if(!r||!e.isTokenNode(r))return-1;const u=r.value;if(u[0]!==n.TokenType.Number)return-1;return numberToCalculation(o,Math.log(u[4].value))}if(2===t.length){const r=t[0];if(!r||!e.isTokenNode(r))return-1;const u=r.value;if(u[0]!==n.TokenType.Number)return-1;const i=t[1];if(!i||!e.isTokenNode(i))return-1;const a=i.value;if(a[0]!==n.TokenType.Number)return-1;return numberToCalculation(o,Math.log(u[4].value)/Math.log(a[4].value))}return-1}const b=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(o,t){const r=resolveGlobalsAndConstants([...o.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t),u=[],i=[],a=[];{let o=u;for(let t=0;t!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t);let u="";const i=[],a=[];{let o=i;for(let t=0;t!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t);if(1===r.length&&e.isTokenNode(r[0]))return{inputs:[r[0]],operation:unary};let u=0;for(;u!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t),i=solve(calc$1(new e.FunctionNode([n.TokenType.Function,"calc(",-1,-1,{value:"calc"}],[n.TokenType.CloseParen,")",-1,-1,void 0],u),t));return-1===i?-1:r(o,i)}function twoCommaSeparatedNodesSolver(o,t,r){const u=resolveGlobalsAndConstants([...o.value.filter((n=>!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t),i=[],a=[];{let o=i;for(let t=0;t!e.isCommentNode(n)&&!e.isWhitespaceNode(n)))],t),i=[];{const o=[];let r=[];for(let t=0;t{if(e.isFunctionNode(n)){const e=b.get(toLowerCaseAZ(n.getName()));if(e){const r=patchCalcResult(solve(e(n,t)),o);if(-1!==r)return r}}}))}const h=new Set(b.keys());exports.calc=function calc(o,t){return calcFromComponentValues(e.parseCommaSeparatedListOfComponentValues(n.tokenize({css:o}),{}),t).map((e=>e.map((e=>n.stringify(...e.tokens()))).join(""))).join(",")},exports.calcFromComponentValues=calcFromComponentValues,exports.mathFunctionNames=h;