(function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o.pather={}))})(this,function(o){"use strict";var x=Object.defineProperty;var y=(o,e,c)=>e in o?x(o,e,{enumerable:!0,configurable:!0,writable:!0,value:c}):o[e]=c;var r=(o,e,c)=>(y(o,typeof e!="symbol"?e+"":e,c),c);class e{constructor(t=null,s){r(this,"g",0);r(this,"h",0);r(this,"equals",t=>e.equals(this,t));this.parent=t,this.position=s}static equals(t,s){return!t.position||!s.position?!1:t.x===s.x&&t.y===s.y}get f(){return this.g+this.h}get x(){return this.position.x}set x(t){this.position.x=t}get y(){return this.position.y}set y(t){this.position.y=t}}const c=h=>{const t=[];let s=h;for(;s.parent;)t.push(s),s=s.parent;return t},d=(h,t=!1)=>{const s=h.x,i=h.y;let n=[{x:0,y:-1},{x:0,y:1},{x:-1,y:0},{x:1,y:0}];return t&&(n=n.concat([{x:-1,y:-1},{x:1,y:-1},{x:-1,y:1},{x:1,y:1}])),n.map(a=>new e(h,{x:s+a.x,y:i+a.y}))},l={DEFAULT:(h,t)=>Math.abs(h.x-t.x)+Math.abs(h.y-t.y)},u={DEFAULT:(h,t)=>h.equals(t)},f=99999;class p{constructor(t){r(this,"possibleNodes");r(this,"checkedNodes");r(this,"start");r(this,"end");r(this,"iterations");r(this,"config");r(this,"initialState",()=>{const s=new e(null,this.config.startPos),i=new e(null,this.config.endPos),n=[],a=[];return n.push(s),{iterations:0,start:s,end:i,possibleNodes:n,checkedNodes:a}});r(this,"findPath",()=>{if(this.iterations!==0){const{start:t,end:s,iterations:i,possibleNodes:n,checkedNodes:a}=this.initialState();this.possibleNodes=n,this.checkedNodes=a,this.start=t,this.end=s,this.iterations=i}for(;this.possibleNodes.length;){if(this.iterations>=this.config.maxIterations)return;const t=this.checkNode();if(t!=null&&t.path)return t.path;this.iterations++}});r(this,"checkNode",()=>{this.possibleNodes.sort((i,n)=>n.f-i.f);const t=this.possibleNodes.pop();if(!t)return;if(this.checkedNodes.push(t),this.config.isDone(t,this.end))return{path:c(t).reverse()};const s=d(t,this.config.diagonal);for(let i of s)this.config.isOutOfBounds(i)||this.config.wouldCollide(i)||this.possibleNodes.filter(n=>n.equals(i)).length>0||this.checkedNodes.filter(n=>n.equals(i)).length>0||(i.g=t.g+1,i.h=this.config.heuristic(i,this.end),this.possibleNodes.push(i))});this.config={...t,heuristic:t.heuristic||l.DEFAULT,diagonal:t.diagonal||!1,maxIterations:t.maxIterations||f,isDone:t.isDone||u.DEFAULT};const{start:s,end:i,iterations:n,possibleNodes:a,checkedNodes:g}=this.initialState();this.possibleNodes=a,this.checkedNodes=g,this.start=s,this.end=i,this.iterations=n}*findPathGen(){if(this.iterations!==0){const{start:t,end:s,iterations:i,possibleNodes:n,checkedNodes:a}=this.initialState();this.possibleNodes=n,this.checkedNodes=a,this.start=t,this.end=s,this.iterations=i}for(;this.possibleNodes.length;){if(this.iterations>=this.config.maxIterations)return;const t=this.checkNode();if(this.iterations++,yield{solution:t,aStar:this},t!=null&&t.path)return}}}const N=(...h)=>new p(...h).findPath();o.PathNode=e,o.findPath=N,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});