/*
 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const CSS=require("css-tree");var CssLib=function(nodeDeps,appCtx){this._ast=null;this._appCtx=appCtx;this._options={positions:true,parseCustomProperty:true,filename:" "};this._options.onParseError=this._onParseError.bind(this);this._listeners={}};CssLib.prototype.addListener=function(code,fn){this._listeners[code]=fn;return this};CssLib.prototype.parse=function(data,fname,pos){this._file=fname;this._error=false;this._pos=pos;this._ast=CSS.parse(data,this._options);if(this._error){var fn=this._listeners.error;if(fn){fn(this._error)}else{throw this._error}}CSS.toPlainObject(this._ast);this._walk()};CssLib.prototype._onNode=function(node,item,list){var fn=this._listeners[node.type];if(fn){fn(node,this._ast,list)}};CssLib.prototype._walk=function(){CSS.walk(this._ast,this._onNode.bind(this))};CssLib.prototype._onParseError=function(error){if(this._appCtx.verboseMode||this._appCtx.debugMode){if(this._file){console.log("File -> "+this._file)}let m=this.formatMsg(error);console.log("["+(error.line+this._pos.row-1)+", "+(error.column+this._pos.col-1)+"] : "+m)}if(!this._error){this._error=[]}this._error.push(error)};CssLib.prototype.formatMsg=function(err){var m,m2,a,l,i;a=err.formattedMessage.split("\n");m=a[0];if(err.name==="SyntaxError"){for(i=1;i<a.length;i++){l=a[i].trim();if(l.startsWith("----")){m2=a[i-1];if(m2){i=m2.indexOf("|");if(i>=0){m2=m2.substring(i+1)}m+="  (near: '"+m2.trim()+"')";break}}}}else{this._appCtx.assert("CSS PARSER - type = "+err.name+" ("+this._file+") ;")}return m};function _displayAst(obj,title){if(title){console.log("\n\n-----------------------");console.log("     "+title);console.log("-----------------------")}console.log(JSON.stringify(obj,null,4))};function _dumpSelector(node,indent){var a,i,sel,s;indent=indent||"";console.log(indent+node.type);a=node.children;for(i=0;i<a.length;i++){sel=a[i];s=indent+"  "+sel.type+"  ";if(sel.type==="TypeSelector"||sel.type==="IdSelector"||sel.type==="ClassSelector"||sel.type==="PseudoClassSelector"){console.log(s+"'"+sel.name+"'")}else{console.log(s+"'"+sel.value+"'")}}};module.exports=CssLib;