const{hasOwnProperty:t}=Object.prototype,e=function(){};function n(t){return"function"==typeof t?t:e}function r(t,e){return function(n,r,l){n.type===e&&t.call(this,n,r,l)}}function l(e,n){const r=n.structure,l=[];for(const e in r){if(!1===t.call(r,e))continue;let n=r[e];const o={name:e,type:!1,nullable:!1};Array.isArray(n)||(n=[n]);for(const t of n)null===t?o.nullable=!0:"string"==typeof t?o.type="node":Array.isArray(t)&&(o.type="list");o.type&&l.push(o)}return l.length?{context:n.walkContext,fields:l}:null}function o(t,e){const n=t.fields.slice(),r=t.context,l="string"==typeof r;return e&&n.reverse(),function(t,o,i,u){let s;l&&(s=o[r],o[r]=t);for(const r of n){const n=t[r.name];if(!r.nullable||n)if("list"===r.type){if(e?n.reduceRight(u,!1):n.reduce(u,!1))return!0}else if(i(n))return!0}l&&(o[r]=s)}}function i({StyleSheet:t,Atrule:e,Rule:n,Block:r,DeclarationList:l}){return{Atrule:{StyleSheet:t,Atrule:e,Rule:n,Block:r},Rule:{StyleSheet:t,Atrule:e,Rule:n,Block:r},Declaration:{StyleSheet:t,Atrule:e,Rule:n,Block:r,DeclarationList:l}}}function u(u){const s=function(e){const n={};for(const r in e.node)if(t.call(e.node,r)){const t=e.node[r];if(!t.structure)throw new Error("Missed `structure` field in `"+r+"` node type definition");n[r]=l(0,t)}return n}(u),c={},f={},a=Symbol("break-walk"),y=Symbol("skip-node");for(const e in s)t.call(s,e)&&null!==s[e]&&(c[e]=o(s[e],!1),f[e]=o(s[e],!0));const p=i(c),h=i(f),d=function(t,l){function o(t,e,n){const r=i.call(k,t,e,n);return r===a||r!==y&&(!(!d.hasOwnProperty(t.type)||!d[t.type](t,k,o,v))||u.call(k,t,e,n)===a)}let i=e,u=e,d=c,v=(t,e,n,r)=>t||o(e,n,r);const k={break:a,skip:y,root:t,stylesheet:null,atrule:null,atrulePrelude:null,rule:null,selector:null,block:null,declaration:null,function:null};if("function"==typeof l)i=l;else if(l&&(i=n(l.enter),u=n(l.leave),l.reverse&&(d=f),l.visit)){if(p.hasOwnProperty(l.visit))d=l.reverse?h[l.visit]:p[l.visit];else if(!s.hasOwnProperty(l.visit))throw new Error("Bad value `"+l.visit+"` for `visit` option (should be: "+Object.keys(s).sort().join(", ")+")");i=r(i,l.visit),u=r(u,l.visit)}if(i===e&&u===e)throw new Error("Neither `enter` nor `leave` walker handler is set or both aren't a function");o(t)};return d.break=a,d.skip=y,d.find=function(t,e){let n=null;return d(t,function(t,r,l){if(e.call(this,t,r,l))return n=t,a}),n},d.findLast=function(t,e){let n=null;return d(t,{reverse:!0,enter(t,r,l){if(e.call(this,t,r,l))return n=t,a}}),n},d.findAll=function(t,e){const n=[];return d(t,function(t,r,l){e.call(this,t,r,l)&&n.push(t)}),n},d}export{u as createWalker};