import t from"postcss-value-parser";import n from"vm";function e(t){return t*(180/Math.PI)}const r={turn:function(t){return 2*t*Math.PI},deg:function(t){return t*(Math.PI/180)},grad:function(t){return t*(Math.PI/200)}};function u(t){return"word"===t.type}const i=["+","-","*","/"];var o;function a(t){return!Number.isNaN(t)&&Number.isFinite(t)}function s(e,u=!1){let l=!0;const f=[];if(e.filter((t=>"function"===t.type)).forEach((t=>{var n;if(!l)return;if(""!==t.value)return void(l=!1);const e=s(t.nodes.slice(0),u),r=1===e.length,i=Number((null==(n=e[0])?void 0:n.value)||"");r&&"word"===e[0].type&&!Number.isNaN(i)?(c(t),t.value=e[0].value):l=!1})),!l)return e;const v=e.filter((t=>"word"===t.type||i.includes(t.value)));let m=o.Number;const h=[];let N;const b=(t,n,e)=>{if(m===n){if(n===o.Number){const n=e||"";h.includes(n)||h.push({number:t,unit:n,index:f.length})}f.push(t),m=n===o.Number?o.Operation:o.Number}else l=!1};for(let n=0,e=v.length;nt.unit)));if(e.size>1)if(e.has("")){if(2!==e.size)throw new Error;[t]=Array.from(e).filter((t=>""!==t))}else h.forEach((t=>{if("rad"!==t.unit){const n=r[t.unit](Number(t.number));if(!a(n))throw new Error;f[t.index]=n.toString()}}));const u=n.createContext({result:NaN});new n.Script(`result = ${f.join(" ")}`).runInContext(u),"number"==typeof u.result&&a(u.result)&&(t&&(u.result=r[t](u.result)),a(u.result)&&(d=u.result))}catch(t){}if(void 0!==d){let t=d.toString();N&&(t+=N);const n=e[0].sourceIndex,r=t.length;e.length=0,e.push({type:"word",value:t,sourceIndex:n,sourceEndIndex:r})}return e}function c(t){delete t.nodes;const n=t;return n.type="word",n}function l(t,n){if(!Number.isNaN(t)){if(t>Number.MAX_SAFE_INTEGER)return"infinity";if(t{if("function"!==t.type||"asin"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.asin(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"acos(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"acos"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.acos(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan"!==t.value)return;const n=v(t,!1);if(!n)return;const[r,u]=n;let i=Math.atan(u);Number.isNaN(i)||"number"!=typeof i||(i=`${l(e(i),2)}deg`),r.value=i+""})),r.toString()}},{check:"atan2(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"atan2"!==t.value)return;const n=t.nodes.findIndex((t=>"div"===t.type&&","===t.value));if(n<0)return;let r=t.nodes.slice(0,n).filter(u),i=t.nodes.slice(n+1).filter(u);if(0===r.length||0===i.length)return;if(r.length>1&&(r=s(r,!0)),i.length>1&&(i=s(i,!0)),1!==r.length||1!==i.length)return;const o=f(r[0].value),a=f(i[0].value);if(!o||!a)return;if(o.unit!==a.unit)return;let v=Math.atan2(o.number,a.number);Number.isNaN(v)||"number"!=typeof v||(v=`${l(e(v),2)}deg`);c(t).value=v+""})),r.toString()}},{check:"sin(",transform:function(n){const e=t(n.value);return e.walk((t=>{if("function"!==t.type||"sin"!==t.value)return;const n=v(t);if(!n)return;const[e,r]=n;e.value=l(Math.sin(r),5)})),e.toString()}},{check:"cos(",transform:function(n){const e=t(n.value);return e.walk((t=>{if("function"!==t.type||"cos"!==t.value)return;const n=v(t);if(!n)return;const[e,r]=n;e.value=l(Math.cos(r),5)})),e.toString()}},{check:"tan(",transform:function(n){const r=t(n.value);return r.walk((t=>{if("function"!==t.type||"tan"!==t.value)return;const n=v(t);if(!n)return;const[r,u]=n,i=Number(l(e(u),2)),o=i/90;r.value=i%90==0&&o%2!=0?o>0?"infinity":"-infinity":l(Math.tan(u),5)})),r.toString()}}],h=t=>{const n=Object.assign({preserve:!1},t);return{postcssPlugin:"postcss-trigonometric-functions",Declaration(t){const e=m.filter((n=>t.value.includes(n.check)));if(!t||0===e.length)return;const r=t.clone();e.forEach((t=>{const n=t.transform(r);n&&(r.value=n)})),t.value!==r.value&&(n.preserve?t.cloneBefore({value:r.value}):t.value=r.value)}}};h.postcss=!0;export{h as default};