UNPKG

chess-analysis-board

Version:

A comprehensive chess analysis board React component with move navigation, variation support, PGN import/export, and customizable keyboard shortcuts

58 lines 228 kB
"use strict";var bv=Object.defineProperty;var yv=(i,t,s)=>t in i?bv(i,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[t]=s;var Ne=(i,t,s)=>yv(i,typeof t!="symbol"?t+"":t,s);const P=require("react");function $v(i){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const s in i)if(s!=="default"){const a=Object.getOwnPropertyDescriptor(i,s);Object.defineProperty(t,s,a.get?a:{enumerable:!0,get:()=>i[s]})}}return t.default=i,Object.freeze(t)}const xn=$v(P);var Sn={exports:{}},Pr={};/** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var $a;function Cv(){if($a)return Pr;$a=1;var i=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function s(a,l,u){var p=null;if(u!==void 0&&(p=""+u),l.key!==void 0&&(p=""+l.key),"key"in l){u={};for(var g in l)g!=="key"&&(u[g]=l[g])}else u=l;return l=u.ref,{$$typeof:i,type:a,key:p,ref:l!==void 0?l:null,props:u}}return Pr.Fragment=t,Pr.jsx=s,Pr.jsxs=s,Pr}var Ir={};/** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var Ca;function Sv(){return Ca||(Ca=1,process.env.NODE_ENV!=="production"&&function(){function i(R){if(R==null)return null;if(typeof R=="function")return R.$$typeof===he?null:R.displayName||R.name||null;if(typeof R=="string")return R;switch(R){case j:return"Fragment";case U:return"Profiler";case W:return"StrictMode";case G:return"Suspense";case d:return"SuspenseList";case Ce:return"Activity"}if(typeof R=="object")switch(typeof R.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),R.$$typeof){case x:return"Portal";case K:return(R.displayName||"Context")+".Provider";case q:return(R._context.displayName||"Context")+".Consumer";case ee:var re=R.render;return R=R.displayName,R||(R=re.displayName||re.name||"",R=R!==""?"ForwardRef("+R+")":"ForwardRef"),R;case X:return re=R.displayName||null,re!==null?re:i(R.type)||"Memo";case r:re=R._payload,R=R._init;try{return i(R(re))}catch{}}return null}function t(R){return""+R}function s(R){try{t(R);var re=!1}catch{re=!0}if(re){re=console;var Se=re.error,$e=typeof Symbol=="function"&&Symbol.toStringTag&&R[Symbol.toStringTag]||R.constructor.name||"Object";return Se.call(re,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",$e),t(R)}}function a(R){if(R===j)return"<>";if(typeof R=="object"&&R!==null&&R.$$typeof===r)return"<...>";try{var re=i(R);return re?"<"+re+">":"<...>"}catch{return"<...>"}}function l(){var R=xe.A;return R===null?null:R.getOwner()}function u(){return Error("react-stack-top-frame")}function p(R){if(ge.call(R,"key")){var re=Object.getOwnPropertyDescriptor(R,"key").get;if(re&&re.isReactWarning)return!1}return R.key!==void 0}function g(R,re){function Se(){te||(te=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",re))}Se.isReactWarning=!0,Object.defineProperty(R,"key",{get:Se,configurable:!0})}function b(){var R=i(this.type);return J[R]||(J[R]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),R=this.props.ref,R!==void 0?R:null}function S(R,re,Se,$e,Re,je,Ye,oe){return Se=je.ref,R={$$typeof:A,type:R,key:re,props:je,_owner:Re},(Se!==void 0?Se:null)!==null?Object.defineProperty(R,"ref",{enumerable:!1,get:b}):Object.defineProperty(R,"ref",{enumerable:!1,value:null}),R._store={},Object.defineProperty(R._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(R,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(R,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:Ye}),Object.defineProperty(R,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:oe}),Object.freeze&&(Object.freeze(R.props),Object.freeze(R)),R}function E(R,re,Se,$e,Re,je,Ye,oe){var we=re.children;if(we!==void 0)if($e)if(De(we)){for($e=0;$e<we.length;$e++)N(we[$e]);Object.freeze&&Object.freeze(we)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else N(we);if(ge.call(re,"key")){we=i(R);var Pe=Object.keys(re).filter(function(Ae){return Ae!=="key"});$e=0<Pe.length?"{key: someKey, "+Pe.join(": ..., ")+": ...}":"{key: someKey}",et[we+$e]||(Pe=0<Pe.length?"{"+Pe.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`,$e,we,Pe,we),et[we+$e]=!0)}if(we=null,Se!==void 0&&(s(Se),we=""+Se),p(re)&&(s(re.key),we=""+re.key),"key"in re){Se={};for(var Ue in re)Ue!=="key"&&(Se[Ue]=re[Ue])}else Se=re;return we&&g(Se,typeof R=="function"?R.displayName||R.name||"Unknown":R),S(R,we,je,Re,l(),Se,Ye,oe)}function N(R){typeof R=="object"&&R!==null&&R.$$typeof===A&&R._store&&(R._store.validated=1)}var _=P,A=Symbol.for("react.transitional.element"),x=Symbol.for("react.portal"),j=Symbol.for("react.fragment"),W=Symbol.for("react.strict_mode"),U=Symbol.for("react.profiler"),q=Symbol.for("react.consumer"),K=Symbol.for("react.context"),ee=Symbol.for("react.forward_ref"),G=Symbol.for("react.suspense"),d=Symbol.for("react.suspense_list"),X=Symbol.for("react.memo"),r=Symbol.for("react.lazy"),Ce=Symbol.for("react.activity"),he=Symbol.for("react.client.reference"),xe=_.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,ge=Object.prototype.hasOwnProperty,De=Array.isArray,ie=console.createTask?console.createTask:function(){return null};_={"react-stack-bottom-frame":function(R){return R()}};var te,J={},fe=_["react-stack-bottom-frame"].bind(_,u)(),Te=ie(a(u)),et={};Ir.Fragment=j,Ir.jsx=function(R,re,Se,$e,Re){var je=1e4>xe.recentlyCreatedOwnerStacks++;return E(R,re,Se,!1,$e,Re,je?Error("react-stack-top-frame"):fe,je?ie(a(R)):Te)},Ir.jsxs=function(R,re,Se,$e,Re){var je=1e4>xe.recentlyCreatedOwnerStacks++;return E(R,re,Se,!0,$e,Re,je?Error("react-stack-top-frame"):fe,je?ie(a(R)):Te)}}()),Ir}var Sa;function wv(){return Sa||(Sa=1,process.env.NODE_ENV==="production"?Sn.exports=Cv():Sn.exports=Sv()),Sn.exports}var m=wv(),nl=Symbol.for("immer-nothing"),wa=Symbol.for("immer-draftable"),dt=Symbol.for("immer-state"),_v=process.env.NODE_ENV!=="production"?[function(i){return`The plugin for '${i}' has not been loaded into Immer. To enable the plugin, import and call \`enable${i}()\` when initializing your application.`},function(i){return`produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${i}'`},"This object has been frozen and should not be mutated",function(i){return"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+i},"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.","Immer forbids circular references","The first or second argument to `produce` must be a function","The third argument to `produce` must be a function or undefined","First argument to `createDraft` must be a plain object, an array, or an immerable object","First argument to `finishDraft` must be a draft returned by `createDraft`",function(i){return`'current' expects a draft, got: ${i}`},"Object.defineProperty() cannot be used on an Immer draft","Object.setPrototypeOf() cannot be used on an Immer draft","Immer only supports deleting array indices","Immer only supports setting array indices and the 'length' property",function(i){return`'original' expects a draft, got: ${i}`}]:[];function ot(i,...t){if(process.env.NODE_ENV!=="production"){const s=_v[i],a=typeof s=="function"?s.apply(null,t):s;throw new Error(`[Immer] ${a}`)}throw new Error(`[Immer] minified error nr: ${i}. Full error at: https://bit.ly/3cXEKWf`)}var yr=Object.getPrototypeOf;function $r(i){return!!i&&!!i[dt]}function er(i){var t;return i?sl(i)||Array.isArray(i)||!!i[wa]||!!((t=i.constructor)!=null&&t[wa])||Ln(i)||Mn(i):!1}var Ev=Object.prototype.constructor.toString();function sl(i){if(!i||typeof i!="object")return!1;const t=yr(i);if(t===null)return!0;const s=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return s===Object?!0:typeof s=="function"&&Function.toString.call(s)===Ev}function jn(i,t){Rn(i)===0?Reflect.ownKeys(i).forEach(s=>{t(s,i[s],i)}):i.forEach((s,a)=>t(a,s,i))}function Rn(i){const t=i[dt];return t?t.type_:Array.isArray(i)?1:Ln(i)?2:Mn(i)?3:0}function Os(i,t){return Rn(i)===2?i.has(t):Object.prototype.hasOwnProperty.call(i,t)}function il(i,t,s){const a=Rn(i);a===2?i.set(t,s):a===3?i.add(s):i[t]=s}function Ov(i,t){return i===t?i!==0||1/i===1/t:i!==i&&t!==t}function Ln(i){return i instanceof Map}function Mn(i){return i instanceof Set}function Jt(i){return i.copy_||i.base_}function xs(i,t){if(Ln(i))return new Map(i);if(Mn(i))return new Set(i);if(Array.isArray(i))return Array.prototype.slice.call(i);const s=sl(i);if(t===!0||t==="class_only"&&!s){const a=Object.getOwnPropertyDescriptors(i);delete a[dt];let l=Reflect.ownKeys(a);for(let u=0;u<l.length;u++){const p=l[u],g=a[p];g.writable===!1&&(g.writable=!0,g.configurable=!0),(g.get||g.set)&&(a[p]={configurable:!0,writable:!0,enumerable:g.enumerable,value:i[p]})}return Object.create(yr(i),a)}else{const a=yr(i);if(a!==null&&s)return{...i};const l=Object.create(a);return Object.assign(l,i)}}function Wr(i,t=!1){return Fn(i)||$r(i)||!er(i)||(Rn(i)>1&&(i.set=i.add=i.clear=i.delete=xv),Object.freeze(i),t&&Object.entries(i).forEach(([s,a])=>Wr(a,!0))),i}function xv(){ot(2)}function Fn(i){return Object.isFrozen(i)}var Tv={};function tr(i){const t=Tv[i];return t||ot(0,i),t}var Kr;function ol(){return Kr}function Dv(i,t){return{drafts_:[],parent_:i,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function _a(i,t){t&&(tr("Patches"),i.patches_=[],i.inversePatches_=[],i.patchListener_=t)}function Ts(i){Ds(i),i.drafts_.forEach(kv),i.drafts_=null}function Ds(i){i===Kr&&(Kr=i.parent_)}function Ea(i){return Kr=Dv(Kr,i)}function kv(i){const t=i[dt];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function Oa(i,t){t.unfinalizedDrafts_=t.drafts_.length;const s=t.drafts_[0];return i!==void 0&&i!==s?(s[dt].modified_&&(Ts(t),ot(4)),er(i)&&(i=An(t,i),t.parent_||Pn(t,i)),t.patches_&&tr("Patches").generateReplacementPatches_(s[dt].base_,i,t.patches_,t.inversePatches_)):i=An(t,s,[]),Ts(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),i!==nl?i:void 0}function An(i,t,s){if(Fn(t))return t;const a=t[dt];if(!a)return jn(t,(l,u)=>xa(i,a,t,l,u,s)),t;if(a.scope_!==i)return t;if(!a.modified_)return Pn(i,a.base_,!0),a.base_;if(!a.finalized_){a.finalized_=!0,a.scope_.unfinalizedDrafts_--;const l=a.copy_;let u=l,p=!1;a.type_===3&&(u=new Set(l),l.clear(),p=!0),jn(u,(g,b)=>xa(i,a,l,g,b,s,p)),Pn(i,l,!1),s&&i.patches_&&tr("Patches").generatePatches_(a,s,i.patches_,i.inversePatches_)}return a.copy_}function xa(i,t,s,a,l,u,p){if(process.env.NODE_ENV!=="production"&&l===s&&ot(5),$r(l)){const g=u&&t&&t.type_!==3&&!Os(t.assigned_,a)?u.concat(a):void 0,b=An(i,l,g);if(il(s,a,b),$r(b))i.canAutoFreeze_=!1;else return}else p&&s.add(l);if(er(l)&&!Fn(l)){if(!i.immer_.autoFreeze_&&i.unfinalizedDrafts_<1)return;An(i,l),(!t||!t.scope_.parent_)&&typeof a!="symbol"&&Object.prototype.propertyIsEnumerable.call(s,a)&&Pn(i,l)}}function Pn(i,t,s=!1){!i.parent_&&i.immer_.autoFreeze_&&i.canAutoFreeze_&&Wr(t,s)}function Nv(i,t){const s=Array.isArray(i),a={type_:s?1:0,scope_:t?t.scope_:ol(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:i,draft_:null,copy_:null,revoke_:null,isManual_:!1};let l=a,u=Bs;s&&(l=[a],u=qr);const{revoke:p,proxy:g}=Proxy.revocable(l,u);return a.draft_=g,a.revoke_=p,g}var Bs={get(i,t){if(t===dt)return i;const s=Jt(i);if(!Os(s,t))return jv(i,s,t);const a=s[t];return i.finalized_||!er(a)?a:a===us(i.base_,t)?(ds(i),i.copy_[t]=Ns(a,i)):a},has(i,t){return t in Jt(i)},ownKeys(i){return Reflect.ownKeys(Jt(i))},set(i,t,s){const a=al(Jt(i),t);if(a!=null&&a.set)return a.set.call(i.draft_,s),!0;if(!i.modified_){const l=us(Jt(i),t),u=l==null?void 0:l[dt];if(u&&u.base_===s)return i.copy_[t]=s,i.assigned_[t]=!1,!0;if(Ov(s,l)&&(s!==void 0||Os(i.base_,t)))return!0;ds(i),ks(i)}return i.copy_[t]===s&&(s!==void 0||t in i.copy_)||Number.isNaN(s)&&Number.isNaN(i.copy_[t])||(i.copy_[t]=s,i.assigned_[t]=!0),!0},deleteProperty(i,t){return us(i.base_,t)!==void 0||t in i.base_?(i.assigned_[t]=!1,ds(i),ks(i)):delete i.assigned_[t],i.copy_&&delete i.copy_[t],!0},getOwnPropertyDescriptor(i,t){const s=Jt(i),a=Reflect.getOwnPropertyDescriptor(s,t);return a&&{writable:!0,configurable:i.type_!==1||t!=="length",enumerable:a.enumerable,value:s[t]}},defineProperty(){ot(11)},getPrototypeOf(i){return yr(i.base_)},setPrototypeOf(){ot(12)}},qr={};jn(Bs,(i,t)=>{qr[i]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}});qr.deleteProperty=function(i,t){return process.env.NODE_ENV!=="production"&&isNaN(parseInt(t))&&ot(13),qr.set.call(this,i,t,void 0)};qr.set=function(i,t,s){return process.env.NODE_ENV!=="production"&&t!=="length"&&isNaN(parseInt(t))&&ot(14),Bs.set.call(this,i[0],t,s,i[0])};function us(i,t){const s=i[dt];return(s?Jt(s):i)[t]}function jv(i,t,s){var l;const a=al(t,s);return a?"value"in a?a.value:(l=a.get)==null?void 0:l.call(i.draft_):void 0}function al(i,t){if(!(t in i))return;let s=yr(i);for(;s;){const a=Object.getOwnPropertyDescriptor(s,t);if(a)return a;s=yr(s)}}function ks(i){i.modified_||(i.modified_=!0,i.parent_&&ks(i.parent_))}function ds(i){i.copy_||(i.copy_=xs(i.base_,i.scope_.immer_.useStrictShallowCopy_))}var Av=class{constructor(i){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(t,s,a)=>{if(typeof t=="function"&&typeof s!="function"){const u=s;s=t;const p=this;return function(b=u,...S){return p.produce(b,E=>s.call(this,E,...S))}}typeof s!="function"&&ot(6),a!==void 0&&typeof a!="function"&&ot(7);let l;if(er(t)){const u=Ea(this),p=Ns(t,void 0);let g=!0;try{l=s(p),g=!1}finally{g?Ts(u):Ds(u)}return _a(u,a),Oa(l,u)}else if(!t||typeof t!="object"){if(l=s(t),l===void 0&&(l=t),l===nl&&(l=void 0),this.autoFreeze_&&Wr(l,!0),a){const u=[],p=[];tr("Patches").generateReplacementPatches_(t,l,u,p),a(u,p)}return l}else ot(1,t)},this.produceWithPatches=(t,s)=>{if(typeof t=="function")return(p,...g)=>this.produceWithPatches(p,b=>t(b,...g));let a,l;return[this.produce(t,s,(p,g)=>{a=p,l=g}),a,l]},typeof(i==null?void 0:i.autoFreeze)=="boolean"&&this.setAutoFreeze(i.autoFreeze),typeof(i==null?void 0:i.useStrictShallowCopy)=="boolean"&&this.setUseStrictShallowCopy(i.useStrictShallowCopy)}createDraft(i){er(i)||ot(8),$r(i)&&(i=Pv(i));const t=Ea(this),s=Ns(i,void 0);return s[dt].isManual_=!0,Ds(t),s}finishDraft(i,t){const s=i&&i[dt];(!s||!s.isManual_)&&ot(9);const{scope_:a}=s;return _a(a,t),Oa(void 0,a)}setAutoFreeze(i){this.autoFreeze_=i}setUseStrictShallowCopy(i){this.useStrictShallowCopy_=i}applyPatches(i,t){let s;for(s=t.length-1;s>=0;s--){const l=t[s];if(l.path.length===0&&l.op==="replace"){i=l.value;break}}s>-1&&(t=t.slice(s+1));const a=tr("Patches").applyPatches_;return $r(i)?a(i,t):this.produce(i,l=>a(l,t))}};function Ns(i,t){const s=Ln(i)?tr("MapSet").proxyMap_(i,t):Mn(i)?tr("MapSet").proxySet_(i,t):Nv(i,t);return(t?t.scope_:ol()).drafts_.push(s),s}function Pv(i){return $r(i)||ot(10,i),ll(i)}function ll(i){if(!er(i)||Fn(i))return i;const t=i[dt];let s;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,s=xs(i,t.scope_.immer_.useStrictShallowCopy_)}else s=xs(i,!0);return jn(s,(a,l)=>{il(s,a,ll(l))}),t&&(t.finalized_=!1),s}var ht=new Av,Iv=ht.produce;ht.produceWithPatches.bind(ht);ht.setAutoFreeze.bind(ht);ht.setUseStrictShallowCopy.bind(ht);ht.applyPatches.bind(ht);ht.createDraft.bind(ht);ht.finishDraft.bind(ht);function Rv(i){var t=P.useState(function(){return Wr(typeof i=="function"?i():i,!0)}),s=t[1];return[t[0],P.useCallback(function(a){s(typeof a=="function"?Iv(a):Wr(a))},[])]}function Lv(i){return i!==null?{comment:i,variations:[]}:{variations:[]}}function Mv(i,t,s,a,l){const u={move:i,variations:l};return t&&(u.suffix=t),s&&(u.nag=s),a!==null&&(u.comment=a),u}function Fv(...i){const[t,...s]=i;let a=t;for(const l of s)l!==null&&(a.variations=[l,...l.variations],l.variations=[],a=l);return t}function Bv(i,t){if(t.marker&&t.marker.comment){let s=t.root;for(;;){const a=s.variations[0];if(!a){s.comment=t.marker.comment;break}s=a}}return{headers:i,root:t.root,result:(t.marker&&t.marker.result)??void 0}}function Uv(i,t){function s(){this.constructor=i}s.prototype=t.prototype,i.prototype=new s}function Cr(i,t,s,a){var l=Error.call(this,i);return Object.setPrototypeOf&&Object.setPrototypeOf(l,Cr.prototype),l.expected=t,l.found=s,l.location=a,l.name="SyntaxError",l}Uv(Cr,Error);function hs(i,t,s){return s=s||" ",i.length>t?i:(t-=i.length,s+=s.repeat(t),i+s.slice(0,t))}Cr.prototype.format=function(i){var t="Error: "+this.message;if(this.location){var s=null,a;for(a=0;a<i.length;a++)if(i[a].source===this.location.source){s=i[a].text.split(/\r\n|\n|\r/g);break}var l=this.location.start,u=this.location.source&&typeof this.location.source.offset=="function"?this.location.source.offset(l):l,p=this.location.source+":"+u.line+":"+u.column;if(s){var g=this.location.end,b=hs("",u.line.toString().length," "),S=s[l.line-1],E=l.line===g.line?g.column:S.length+1,N=E-l.column||1;t+=` --> `+p+` `+b+` | `+u.line+" | "+S+` `+b+" | "+hs("",l.column-1," ")+hs("",N,"^")}else t+=` at `+p}return t};Cr.buildMessage=function(i,t){var s={literal:function(S){return'"'+l(S.text)+'"'},class:function(S){var E=S.parts.map(function(N){return Array.isArray(N)?u(N[0])+"-"+u(N[1]):u(N)});return"["+(S.inverted?"^":"")+E.join("")+"]"},any:function(){return"any character"},end:function(){return"end of input"},other:function(S){return S.description}};function a(S){return S.charCodeAt(0).toString(16).toUpperCase()}function l(S){return S.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(E){return"\\x0"+a(E)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(E){return"\\x"+a(E)})}function u(S){return S.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,function(E){return"\\x0"+a(E)}).replace(/[\x10-\x1F\x7F-\x9F]/g,function(E){return"\\x"+a(E)})}function p(S){return s[S.type](S)}function g(S){var E=S.map(p),N,_;if(E.sort(),E.length>0){for(N=1,_=1;N<E.length;N++)E[N-1]!==E[N]&&(E[_]=E[N],_++);E.length=_}switch(E.length){case 1:return E[0];case 2:return E[0]+" or "+E[1];default:return E.slice(0,-1).join(", ")+", or "+E[E.length-1]}}function b(S){return S?'"'+l(S)+'"':"end of input"}return"Expected "+g(i)+" but "+b(t)+" found."};function Wv(i,t){t=t!==void 0?t:{};var s={},a=t.grammarSource,l={pgn:_r},u=_r,p="[",g='"',b="]",S=".",E="O-O-O",N="O-O",_="0-0-0",A="0-0",x="$",j="{",W="}",U=";",q="(",K=")",ee="1-0",G="0-1",d="1/2-1/2",X="*",r=/^[a-zA-Z]/,Ce=/^[^"]/,he=/^[0-9]/,xe=/^[.]/,ge=/^[a-zA-Z1-8\-=]/,De=/^[+#]/,ie=/^[!?]/,te=/^[^}]/,J=/^[^\r\n]/,fe=/^[ \t\r\n]/,Te=Ve("tag pair"),et=be("[",!1),R=be('"',!1),re=be("]",!1),Se=Ve("tag name"),$e=Ie([["a","z"],["A","Z"]],!1,!1),Re=Ve("tag value"),je=Ie(['"'],!0,!1),Ye=Ve("move number"),oe=Ie([["0","9"]],!1,!1),we=be(".",!1),Pe=Ie(["."],!1,!1),Ue=Ve("standard algebraic notation"),Ae=be("O-O-O",!1),We=be("O-O",!1),bt=be("0-0-0",!1),Qe=be("0-0",!1),rt=Ie([["a","z"],["A","Z"],["1","8"],"-","="],!1,!1),at=Ie(["+","#"],!1,!1),Nt=Ve("suffix annotation"),yt=Ie(["!","?"],!1,!1),qt=Ve("NAG"),jt=be("$",!1),$t=Ve("brace comment"),nt=be("{",!1),gt=Ie(["}"],!0,!1),Et=be("}",!1),L=Ve("rest of line comment"),F=be(";",!1),z=Ie(["\r",` `],!0,!1),ce=Ve("variation"),me=be("(",!1),ve=be(")",!1),ne=Ve("game termination marker"),pe=be("1-0",!1),se=be("0-1",!1),Me=be("1/2-1/2",!1),lt=be("*",!1),Ct=Ve("whitespace"),Ot=Ie([" "," ","\r",` `],!1,!1),xt=function(w,T){return Bv(w,T)},pt=function(w){return Object.fromEntries(w)},sr=function(w,T){return[w,T]},ir=function(w,T){return{root:w,marker:T}},or=function(w,T){return Fv(Lv(w),...T.flat())},ar=function(w,T,D,Y,Z){return Mv(w,T,D,Y,Z)},At=function(w){return w},lr=function(w){return w.replace(/[\r\n]+/g," ")},Dt=function(w){return w.trim()},Pt=function(w){return w},cr=function(w,T){return{result:w,comment:T}},O=t.peg$currPos|0,St=[{line:1,column:1}],Xe=O,Ee=t.peg$maxFailExpected||[],M=t.peg$silentFails|0,Le;if(t.startRule){if(!(t.startRule in l))throw new Error(`Can't start parsing from rule "`+t.startRule+'".');u=l[t.startRule]}function be(w,T){return{type:"literal",text:w,ignoreCase:T}}function Ie(w,T,D){return{type:"class",parts:w,inverted:T,ignoreCase:D}}function Je(){return{type:"end"}}function Ve(w){return{type:"other",description:w}}function Sr(w){var T=St[w],D;if(T)return T;if(w>=St.length)D=St.length-1;else for(D=w;!St[--D];);for(T=St[D],T={line:T.line,column:T.column};D<w;)i.charCodeAt(D)===10?(T.line++,T.column=1):T.column++,D++;return St[w]=T,T}function wr(w,T,D){var Y=Sr(w),Z=Sr(T),Oe={source:a,start:{offset:w,line:Y.line,column:Y.column},end:{offset:T,line:Z.line,column:Z.column}};return Oe}function V(w){O<Xe||(O>Xe&&(Xe=O,Ee=[]),Ee.push(w))}function Vr(w,T,D){return new Cr(Cr.buildMessage(w,T),w,T,D)}function _r(){var w,T,D;return w=O,T=Gr(),D=Xr(),w=xt(T,D),w}function Gr(){var w,T,D;for(w=O,T=[],D=Er();D!==s;)T.push(D),D=Er();return D=qe(),w=pt(T),w}function Er(){var w,T,D,Y,Z,Oe,Tt;return M++,w=O,qe(),i.charCodeAt(O)===91?(T=p,O++):(T=s,M===0&&V(et)),T!==s?(qe(),D=Yr(),D!==s?(qe(),i.charCodeAt(O)===34?(Y=g,O++):(Y=s,M===0&&V(R)),Y!==s?(Z=Qr(),i.charCodeAt(O)===34?(Oe=g,O++):(Oe=s,M===0&&V(R)),Oe!==s?(qe(),i.charCodeAt(O)===93?(Tt=b,O++):(Tt=s,M===0&&V(re)),Tt!==s?w=sr(D,Z):(O=w,w=s)):(O=w,w=s)):(O=w,w=s)):(O=w,w=s)):(O=w,w=s),M--,w===s&&M===0&&V(Te),w}function Yr(){var w,T,D;if(M++,w=O,T=[],D=i.charAt(O),r.test(D)?O++:(D=s,M===0&&V($e)),D!==s)for(;D!==s;)T.push(D),D=i.charAt(O),r.test(D)?O++:(D=s,M===0&&V($e));else T=s;return T!==s?w=i.substring(w,O):w=T,M--,w===s&&(T=s,M===0&&V(Se)),w}function Qr(){var w,T,D;for(M++,w=O,T=[],D=i.charAt(O),Ce.test(D)?O++:(D=s,M===0&&V(je));D!==s;)T.push(D),D=i.charAt(O),Ce.test(D)?O++:(D=s,M===0&&V(je));return w=i.substring(w,O),M--,T=s,M===0&&V(Re),w}function Xr(){var w,T,D;return w=O,T=Or(),qe(),D=nn(),D===s&&(D=null),qe(),w=ir(T,D),w}function Or(){var w,T,D,Y;for(w=O,T=fr(),T===s&&(T=null),D=[],Y=xr();Y!==s;)D.push(Y),Y=xr();return w=or(T,D),w}function xr(){var w,T,D,Y,Z,Oe,Tt,Ht;if(w=O,qe(),Jr(),qe(),T=Zr(),T!==s){for(D=en(),D===s&&(D=null),Y=[],Z=Tr();Z!==s;)Y.push(Z),Z=Tr();for(Z=qe(),Oe=fr(),Oe===s&&(Oe=null),Tt=[],Ht=Dr();Ht!==s;)Tt.push(Ht),Ht=Dr();w=ar(T,D,Y,Oe,Tt)}else O=w,w=s;return w}function Jr(){var w,T,D,Y,Z,Oe;for(M++,w=O,T=[],D=i.charAt(O),he.test(D)?O++:(D=s,M===0&&V(oe));D!==s;)T.push(D),D=i.charAt(O),he.test(D)?O++:(D=s,M===0&&V(oe));if(i.charCodeAt(O)===46?(D=S,O++):(D=s,M===0&&V(we)),D!==s){for(Y=qe(),Z=[],Oe=i.charAt(O),xe.test(Oe)?O++:(Oe=s,M===0&&V(Pe));Oe!==s;)Z.push(Oe),Oe=i.charAt(O),xe.test(Oe)?O++:(Oe=s,M===0&&V(Pe));T=[T,D,Y,Z],w=T}else O=w,w=s;return M--,w===s&&(T=s,M===0&&V(Ye)),w}function Zr(){var w,T,D,Y,Z,Oe;if(M++,w=O,T=O,i.substr(O,5)===E?(D=E,O+=5):(D=s,M===0&&V(Ae)),D===s&&(i.substr(O,3)===N?(D=N,O+=3):(D=s,M===0&&V(We)),D===s&&(i.substr(O,5)===_?(D=_,O+=5):(D=s,M===0&&V(bt)),D===s&&(i.substr(O,3)===A?(D=A,O+=3):(D=s,M===0&&V(Qe)),D===s))))if(D=O,Y=i.charAt(O),r.test(Y)?O++:(Y=s,M===0&&V($e)),Y!==s){if(Z=[],Oe=i.charAt(O),ge.test(Oe)?O++:(Oe=s,M===0&&V(rt)),Oe!==s)for(;Oe!==s;)Z.push(Oe),Oe=i.charAt(O),ge.test(Oe)?O++:(Oe=s,M===0&&V(rt));else Z=s;Z!==s?(Y=[Y,Z],D=Y):(O=D,D=s)}else O=D,D=s;return D!==s?(Y=i.charAt(O),De.test(Y)?O++:(Y=s,M===0&&V(at)),Y===s&&(Y=null),D=[D,Y],T=D):(O=T,T=s),T!==s?w=i.substring(w,O):w=T,M--,w===s&&(T=s,M===0&&V(Ue)),w}function en(){var w,T,D;for(M++,w=O,T=[],D=i.charAt(O),ie.test(D)?O++:(D=s,M===0&&V(yt));D!==s;)T.push(D),T.length>=2?D=s:(D=i.charAt(O),ie.test(D)?O++:(D=s,M===0&&V(yt)));return T.length<1?(O=w,w=s):w=T,M--,w===s&&(T=s,M===0&&V(Nt)),w}function Tr(){var w,T,D,Y,Z;if(M++,w=O,qe(),i.charCodeAt(O)===36?(T=x,O++):(T=s,M===0&&V(jt)),T!==s){if(D=O,Y=[],Z=i.charAt(O),he.test(Z)?O++:(Z=s,M===0&&V(oe)),Z!==s)for(;Z!==s;)Y.push(Z),Z=i.charAt(O),he.test(Z)?O++:(Z=s,M===0&&V(oe));else Y=s;Y!==s?D=i.substring(D,O):D=Y,D!==s?w=At(D):(O=w,w=s)}else O=w,w=s;return M--,w===s&&M===0&&V(qt),w}function fr(){var w;return w=tn(),w===s&&(w=rn()),w}function tn(){var w,T,D,Y,Z;if(M++,w=O,i.charCodeAt(O)===123?(T=j,O++):(T=s,M===0&&V(nt)),T!==s){for(D=O,Y=[],Z=i.charAt(O),te.test(Z)?O++:(Z=s,M===0&&V(gt));Z!==s;)Y.push(Z),Z=i.charAt(O),te.test(Z)?O++:(Z=s,M===0&&V(gt));D=i.substring(D,O),i.charCodeAt(O)===125?(Y=W,O++):(Y=s,M===0&&V(Et)),Y!==s?w=lr(D):(O=w,w=s)}else O=w,w=s;return M--,w===s&&(T=s,M===0&&V($t)),w}function rn(){var w,T,D,Y,Z;if(M++,w=O,i.charCodeAt(O)===59?(T=U,O++):(T=s,M===0&&V(F)),T!==s){for(D=O,Y=[],Z=i.charAt(O),J.test(Z)?O++:(Z=s,M===0&&V(z));Z!==s;)Y.push(Z),Z=i.charAt(O),J.test(Z)?O++:(Z=s,M===0&&V(z));D=i.substring(D,O),w=Dt(D)}else O=w,w=s;return M--,w===s&&(T=s,M===0&&V(L)),w}function Dr(){var w,T,D,Y;return M++,w=O,qe(),i.charCodeAt(O)===40?(T=q,O++):(T=s,M===0&&V(me)),T!==s?(D=Or(),D!==s?(qe(),i.charCodeAt(O)===41?(Y=K,O++):(Y=s,M===0&&V(ve)),Y!==s?w=Pt(D):(O=w,w=s)):(O=w,w=s)):(O=w,w=s),M--,w===s&&M===0&&V(ce),w}function nn(){var w,T,D;return M++,w=O,i.substr(O,3)===ee?(T=ee,O+=3):(T=s,M===0&&V(pe)),T===s&&(i.substr(O,3)===G?(T=G,O+=3):(T=s,M===0&&V(se)),T===s&&(i.substr(O,7)===d?(T=d,O+=7):(T=s,M===0&&V(Me)),T===s&&(i.charCodeAt(O)===42?(T=X,O++):(T=s,M===0&&V(lt))))),T!==s?(qe(),D=fr(),D===s&&(D=null),w=cr(T,D)):(O=w,w=s),M--,w===s&&(T=s,M===0&&V(ne)),w}function qe(){var w,T;for(M++,w=[],T=i.charAt(O),fe.test(T)?O++:(T=s,M===0&&V(Ot));T!==s;)w.push(T),T=i.charAt(O),fe.test(T)?O++:(T=s,M===0&&V(Ot));return M--,T=s,M===0&&V(Ct),w}if(Le=u(),t.peg$library)return{peg$result:Le,peg$currPos:O,peg$FAILED:s,peg$maxFailExpected:Ee,peg$maxFailPos:Xe};if(Le!==s&&O===i.length)return Le;throw Le!==s&&O<i.length&&V(Je()),Vr(Ee,Xe<i.length?i.charAt(Xe):null,Xe<i.length?wr(Xe,Xe+1):wr(Xe,Xe))}/** * @license * Copyright (c) 2025, Jeff Hlywa (jhlywa@gmail.com) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */const Tn=0xffffffffffffffffn;function gs(i,t){return(i<<t|i>>64n-t)&0xffffffffffffffffn}function Ta(i,t){return i*t&Tn}function Kv(i){return function(){let t=BigInt(i&Tn),s=BigInt(i>>64n&Tn);const a=Ta(gs(Ta(t,5n),7n),9n);return s^=t,t=(gs(t,24n)^s^s<<16n)&Tn,s=gs(s,37n),i=s<<64n|t,a}}const Bn=Kv(0xa187eb39cdcaed8f31c4b365b102e01en),qv=Array.from({length:2},()=>Array.from({length:6},()=>Array.from({length:128},()=>Bn()))),Hv=Array.from({length:8},()=>Bn()),zv=Array.from({length:16},()=>Bn()),ps=Bn(),Ze="w",ut="b",Be="p",js="n",Dn="b",Mr="r",Wt="q",Ke="k",vs="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";class wn{constructor(t,s){Ne(this,"color");Ne(this,"from");Ne(this,"to");Ne(this,"piece");Ne(this,"captured");Ne(this,"promotion");Ne(this,"flags");Ne(this,"san");Ne(this,"lan");Ne(this,"before");Ne(this,"after");const{color:a,piece:l,from:u,to:p,flags:g,captured:b,promotion:S}=s,E=ze(u),N=ze(p);this.color=a,this.piece=l,this.from=E,this.to=N,this.san=t._moveToSan(s,t._moves({legal:!0})),this.lan=E+N,this.before=t.fen(),t._makeMove(s),this.after=t.fen(),t._undoMove(),this.flags="";for(const _ in le)le[_]&g&&(this.flags+=Qt[_]);b&&(this.captured=b),S&&(this.promotion=S,this.lan+=S)}isCapture(){return this.flags.indexOf(Qt.CAPTURE)>-1}isPromotion(){return this.flags.indexOf(Qt.PROMOTION)>-1}isEnPassant(){return this.flags.indexOf(Qt.EP_CAPTURE)>-1}isKingsideCastle(){return this.flags.indexOf(Qt.KSIDE_CASTLE)>-1}isQueensideCastle(){return this.flags.indexOf(Qt.QSIDE_CASTLE)>-1}isBigPawn(){return this.flags.indexOf(Qt.BIG_PAWN)>-1}}const Ge=-1,Qt={NORMAL:"n",CAPTURE:"c",BIG_PAWN:"b",EP_CAPTURE:"e",PROMOTION:"p",KSIDE_CASTLE:"k",QSIDE_CASTLE:"q",NULL_MOVE:"-"},le={NORMAL:1,CAPTURE:2,BIG_PAWN:4,EP_CAPTURE:8,PROMOTION:16,KSIDE_CASTLE:32,QSIDE_CASTLE:64,NULL_MOVE:128},As={Event:"?",Site:"?",Date:"????.??.??",Round:"?",White:"?",Black:"?",Result:"*"},Vv={WhiteTitle:null,BlackTitle:null,WhiteElo:null,BlackElo:null,WhiteUSCF:null,BlackUSCF:null,WhiteNA:null,BlackNA:null,WhiteType:null,BlackType:null,EventDate:null,EventSponsor:null,Section:null,Stage:null,Board:null,Opening:null,Variation:null,SubVariation:null,ECO:null,NIC:null,Time:null,UTCTime:null,UTCDate:null,TimeControl:null,SetUp:null,FEN:null,Termination:null,Annotator:null,Mode:null,PlyCount:null},Gv={...As,...Vv},ae={a8:0,b8:1,c8:2,d8:3,e8:4,f8:5,g8:6,h8:7,a7:16,b7:17,c7:18,d7:19,e7:20,f7:21,g7:22,h7:23,a6:32,b6:33,c6:34,d6:35,e6:36,f6:37,g6:38,h6:39,a5:48,b5:49,c5:50,d5:51,e5:52,f5:53,g5:54,h5:55,a4:64,b4:65,c4:66,d4:67,e4:68,f4:69,g4:70,h4:71,a3:80,b3:81,c3:82,d3:83,e3:84,f3:85,g3:86,h3:87,a2:96,b2:97,c2:98,d2:99,e2:100,f2:101,g2:102,h2:103,a1:112,b1:113,c1:114,d1:115,e1:116,f1:117,g1:118,h1:119},ms={b:[16,32,17,15],w:[-16,-32,-17,-15]},Da={n:[-18,-33,-31,-14,18,33,31,14],b:[-17,-15,17,15],r:[-16,1,16,-1],q:[-17,-16,-15,1,17,16,15,-1],k:[-17,-16,-15,1,17,16,15,-1]},Yv=[20,0,0,0,0,0,0,24,0,0,0,0,0,0,20,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,24,24,24,24,24,24,56,0,56,24,24,24,24,24,24,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,20,0,0,0,0,0,0,24,0,0,0,0,0,0,20],Qv=[17,0,0,0,0,0,0,16,0,0,0,0,0,0,15,0,0,17,0,0,0,0,0,16,0,0,0,0,0,15,0,0,0,0,17,0,0,0,0,16,0,0,0,0,15,0,0,0,0,0,0,17,0,0,0,16,0,0,0,15,0,0,0,0,0,0,0,0,17,0,0,16,0,0,15,0,0,0,0,0,0,0,0,0,0,17,0,16,0,15,0,0,0,0,0,0,0,0,0,0,0,0,17,16,15,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,-15,-16,-17,0,0,0,0,0,0,0,0,0,0,0,0,-15,0,-16,0,-17,0,0,0,0,0,0,0,0,0,0,-15,0,0,-16,0,0,-17,0,0,0,0,0,0,0,0,-15,0,0,0,-16,0,0,0,-17,0,0,0,0,0,0,-15,0,0,0,0,-16,0,0,0,0,-17,0,0,0,0,-15,0,0,0,0,0,-16,0,0,0,0,0,-17,0,0,-15,0,0,0,0,0,0,-16,0,0,0,0,0,0,-17],Xv={p:1,n:2,b:4,r:8,q:16,k:32},Jv="pnbrqkPNBRQK",ka=[js,Dn,Mr,Wt],Zv=7,e1=6,t1=1,r1=0,_n={[Ke]:le.KSIDE_CASTLE,[Wt]:le.QSIDE_CASTLE},Bt={w:[{square:ae.a1,flag:le.QSIDE_CASTLE},{square:ae.h1,flag:le.KSIDE_CASTLE}],b:[{square:ae.a8,flag:le.QSIDE_CASTLE},{square:ae.h8,flag:le.KSIDE_CASTLE}]},n1={b:t1,w:e1},bs="--";function Zt(i){return i>>4}function Hr(i){return i&15}function cl(i){return"0123456789".indexOf(i)!==-1}function ze(i){const t=Hr(i),s=Zt(i);return"abcdefgh".substring(t,t+1)+"87654321".substring(s,s+1)}function Rr(i){return i===Ze?ut:Ze}function s1(i){const t=i.split(/\s+/);if(t.length!==6)return{ok:!1,error:"Invalid FEN: must contain six space-delimited fields"};const s=parseInt(t[5],10);if(isNaN(s)||s<=0)return{ok:!1,error:"Invalid FEN: move number must be a positive integer"};const a=parseInt(t[4],10);if(isNaN(a)||a<0)return{ok:!1,error:"Invalid FEN: half move counter number must be a non-negative integer"};if(!/^(-|[abcdefgh][36])$/.test(t[3]))return{ok:!1,error:"Invalid FEN: en-passant square is invalid"};if(/[^kKqQ-]/.test(t[2]))return{ok:!1,error:"Invalid FEN: castling availability is invalid"};if(!/^(w|b)$/.test(t[1]))return{ok:!1,error:"Invalid FEN: side-to-move is invalid"};const l=t[0].split("/");if(l.length!==8)return{ok:!1,error:"Invalid FEN: piece data does not contain 8 '/'-delimited rows"};for(let p=0;p<l.length;p++){let g=0,b=!1;for(let S=0;S<l[p].length;S++)if(cl(l[p][S])){if(b)return{ok:!1,error:"Invalid FEN: piece data is invalid (consecutive number)"};g+=parseInt(l[p][S],10),b=!0}else{if(!/^[prnbqkPRNBQK]$/.test(l[p][S]))return{ok:!1,error:"Invalid FEN: piece data is invalid (invalid piece)"};g+=1,b=!1}if(g!==8)return{ok:!1,error:"Invalid FEN: piece data is invalid (too many squares in rank)"}}if(t[3][1]=="3"&&t[1]=="w"||t[3][1]=="6"&&t[1]=="b")return{ok:!1,error:"Invalid FEN: illegal en-passant square"};const u=[{color:"white",regex:/K/g},{color:"black",regex:/k/g}];for(const{color:p,regex:g}of u){if(!g.test(t[0]))return{ok:!1,error:`Invalid FEN: missing ${p} king`};if((t[0].match(g)||[]).length>1)return{ok:!1,error:`Invalid FEN: too many ${p} kings`}}return Array.from(l[0]+l[7]).some(p=>p.toUpperCase()==="P")?{ok:!1,error:"Invalid FEN: some pawns are on the edge rows"}:{ok:!0}}function i1(i,t){const s=i.from,a=i.to,l=i.piece;let u=0,p=0,g=0;for(let b=0,S=t.length;b<S;b++){const E=t[b].from,N=t[b].to,_=t[b].piece;l===_&&s!==E&&a===N&&(u++,Zt(s)===Zt(E)&&p++,Hr(s)===Hr(E)&&g++)}return u>0?p>0&&g>0?ze(s):g>0?ze(s).charAt(1):ze(s).charAt(0):""}function Ut(i,t,s,a,l,u=void 0,p=le.NORMAL){const g=Zt(a);if(l===Be&&(g===Zv||g===r1))for(let b=0;b<ka.length;b++){const S=ka[b];i.push({color:t,from:s,to:a,piece:l,captured:u,promotion:S,flags:p|le.PROMOTION})}else i.push({color:t,from:s,to:a,piece:l,captured:u,flags:p})}function Na(i){let t=i.charAt(0);return t>="a"&&t<="h"?i.match(/[a-h]\d.*[a-h]\d/)?void 0:Be:(t=t.toLowerCase(),t==="o"?Ke:t)}function ys(i){return i.replace(/=/,"").replace(/[+#]?[?!]*$/,"")}class Xt{constructor(t=vs,{skipValidation:s=!1}={}){Ne(this,"_board",new Array(128));Ne(this,"_turn",Ze);Ne(this,"_header",{});Ne(this,"_kings",{w:Ge,b:Ge});Ne(this,"_epSquare",-1);Ne(this,"_halfMoves",0);Ne(this,"_moveNumber",0);Ne(this,"_history",[]);Ne(this,"_comments",{});Ne(this,"_castling",{w:0,b:0});Ne(this,"_hash",0n);Ne(this,"_positionCount",new Map);this.load(t,{skipValidation:s})}clear({preserveHeaders:t=!1}={}){this._board=new Array(128),this._kings={w:Ge,b:Ge},this._turn=Ze,this._castling={w:0,b:0},this._epSquare=Ge,this._halfMoves=0,this._moveNumber=1,this._history=[],this._comments={},this._header=t?this._header:{...Gv},this._hash=this._computeHash(),this._positionCount=new Map,this._header.SetUp=null,this._header.FEN=null}load(t,{skipValidation:s=!1,preserveHeaders:a=!1}={}){let l=t.split(/\s+/);if(l.length>=2&&l.length<6){const g=["-","-","0","1"];t=l.concat(g.slice(-(6-l.length))).join(" ")}if(l=t.split(/\s+/),!s){const{ok:g,error:b}=s1(t);if(!g)throw new Error(b)}const u=l[0];let p=0;this.clear({preserveHeaders:a});for(let g=0;g<u.length;g++){const b=u.charAt(g);if(b==="/")p+=8;else if(cl(b))p+=parseInt(b,10);else{const S=b<"a"?Ze:ut;this._put({type:b.toLowerCase(),color:S},ze(p)),p++}}this._turn=l[1],l[2].indexOf("K")>-1&&(this._castling.w|=le.KSIDE_CASTLE),l[2].indexOf("Q")>-1&&(this._castling.w|=le.QSIDE_CASTLE),l[2].indexOf("k")>-1&&(this._castling.b|=le.KSIDE_CASTLE),l[2].indexOf("q")>-1&&(this._castling.b|=le.QSIDE_CASTLE),this._epSquare=l[3]==="-"?Ge:ae[l[3]],this._halfMoves=parseInt(l[4],10),this._moveNumber=parseInt(l[5],10),this._hash=this._computeHash(),this._updateSetup(t),this._incPositionCount()}fen({forceEnpassantSquare:t=!1}={}){var p,g;let s=0,a="";for(let b=ae.a8;b<=ae.h1;b++){if(this._board[b]){s>0&&(a+=s,s=0);const{color:S,type:E}=this._board[b];a+=S===Ze?E.toUpperCase():E.toLowerCase()}else s++;b+1&136&&(s>0&&(a+=s),b!==ae.h1&&(a+="/"),s=0,b+=8)}let l="";this._castling[Ze]&le.KSIDE_CASTLE&&(l+="K"),this._castling[Ze]&le.QSIDE_CASTLE&&(l+="Q"),this._castling[ut]&le.KSIDE_CASTLE&&(l+="k"),this._castling[ut]&le.QSIDE_CASTLE&&(l+="q"),l=l||"-";let u="-";if(this._epSquare!==Ge)if(t)u=ze(this._epSquare);else{const b=this._epSquare+(this._turn===Ze?16:-16),S=[b+1,b-1];for(const E of S){if(E&136)continue;const N=this._turn;if(((p=this._board[E])==null?void 0:p.color)===N&&((g=this._board[E])==null?void 0:g.type)===Be){this._makeMove({color:N,from:E,to:this._epSquare,piece:Be,captured:Be,flags:le.EP_CAPTURE});const _=!this._isKingAttacked(N);if(this._undoMove(),_){u=ze(this._epSquare);break}}}}return[a,this._turn,l,u,this._halfMoves,this._moveNumber].join(" ")}_pieceKey(t){if(!this._board[t])return 0n;const{color:s,type:a}=this._board[t],l={w:0,b:1}[s],u={p:0,n:1,b:2,r:3,q:4,k:5}[a];return qv[l][u][t]}_epKey(){return this._epSquare===Ge?0n:Hv[this._epSquare&7]}_castlingKey(){const t=this._castling.w>>5|this._castling.b>>3;return zv[t]}_computeHash(){let t=0n;for(let s=ae.a8;s<=ae.h1;s++){if(s&136){s+=7;continue}this._board[s]&&(t^=this._pieceKey(s))}return t^=this._epKey(),t^=this._castlingKey(),this._turn==="b"&&(t^=ps),t}_updateSetup(t){this._history.length>0||(t!==vs?(this._header.SetUp="1",this._header.FEN=t):(this._header.SetUp=null,this._header.FEN=null))}reset(){this.load(vs)}get(t){return this._board[ae[t]]}findPiece(t){var a;const s=[];for(let l=ae.a8;l<=ae.h1;l++){if(l&136){l+=7;continue}!this._board[l]||((a=this._board[l])==null?void 0:a.color)!==t.color||this._board[l].color===t.color&&this._board[l].type===t.type&&s.push(ze(l))}return s}put({type:t,color:s},a){return this._put({type:t,color:s},a)?(this._updateCastlingRights(),this._updateEnPassantSquare(),this._updateSetup(this.fen()),!0):!1}_set(t,s){this._hash^=this._pieceKey(t),this._board[t]=s,this._hash^=this._pieceKey(t)}_put({type:t,color:s},a){if(Jv.indexOf(t.toLowerCase())===-1||!(a in ae))return!1;const l=ae[a];if(t==Ke&&!(this._kings[s]==Ge||this._kings[s]==l))return!1;const u=this._board[l];return u&&u.type===Ke&&(this._kings[u.color]=Ge),this._set(l,{type:t,color:s}),t===Ke&&(this._kings[s]=l),!0}_clear(t){this._hash^=this._pieceKey(t),delete this._board[t]}remove(t){const s=this.get(t);return this._clear(ae[t]),s&&s.type===Ke&&(this._kings[s.color]=Ge),this._updateCastlingRights(),this._updateEnPassantSquare(),this._updateSetup(this.fen()),s}_updateCastlingRights(){var a,l,u,p,g,b,S,E,N,_,A,x;this._hash^=this._castlingKey();const t=((a=this._board[ae.e1])==null?void 0:a.type)===Ke&&((l=this._board[ae.e1])==null?void 0:l.color)===Ze,s=((u=this._board[ae.e8])==null?void 0:u.type)===Ke&&((p=this._board[ae.e8])==null?void 0:p.color)===ut;(!t||((g=this._board[ae.a1])==null?void 0:g.type)!==Mr||((b=this._board[ae.a1])==null?void 0:b.color)!==Ze)&&(this._castling.w&=-65),(!t||((S=this._board[ae.h1])==null?void 0:S.type)!==Mr||((E=this._board[ae.h1])==null?void 0:E.color)!==Ze)&&(this._castling.w&=-33),(!s||((N=this._board[ae.a8])==null?void 0:N.type)!==Mr||((_=this._board[ae.a8])==null?void 0:_.color)!==ut)&&(this._castling.b&=-65),(!s||((A=this._board[ae.h8])==null?void 0:A.type)!==Mr||((x=this._board[ae.h8])==null?void 0:x.color)!==ut)&&(this._castling.b&=-33),this._hash^=this._castlingKey()}_updateEnPassantSquare(){var u,p;if(this._epSquare===Ge)return;const t=this._epSquare+(this._turn===Ze?-16:16),s=this._epSquare+(this._turn===Ze?16:-16),a=[s+1,s-1];if(this._board[t]!==null||this._board[this._epSquare]!==null||((u=this._board[s])==null?void 0:u.color)!==Rr(this._turn)||((p=this._board[s])==null?void 0:p.type)!==Be){this._hash^=this._epKey(),this._epSquare=Ge;return}const l=g=>{var b,S;return!(g&136)&&((b=this._board[g])==null?void 0:b.color)===this._turn&&((S=this._board[g])==null?void 0:S.type)===Be};a.some(l)||(this._hash^=this._epKey(),this._epSquare=Ge)}_attacked(t,s,a){const l=[];for(let u=ae.a8;u<=ae.h1;u++){if(u&136){u+=7;continue}if(this._board[u]===void 0||this._board[u].color!==t)continue;const p=this._board[u],g=u-s;if(g===0)continue;const b=g+119;if(Yv[b]&Xv[p.type]){if(p.type===Be){if(g>0&&p.color===Ze||g<=0&&p.color===ut)if(a)l.push(ze(u));else return!0;continue}if(p.type==="n"||p.type==="k")if(a){l.push(ze(u));continue}else return!0;const S=Qv[b];let E=u+S,N=!1;for(;E!==s;){if(this._board[E]!=null){N=!0;break}E+=S}if(!N)if(a){l.push(ze(u));continue}else return!0}}return a?l:!1}attackers(t,s){return s?this._attacked(s,ae[t],!0):this._attacked(this._turn,ae[t],!0)}_isKingAttacked(t){const s=this._kings[t];return s===-1?!1:this._attacked(Rr(t),s)}hash(){return this._hash.toString(16)}isAttacked(t,s){return this._attacked(s,ae[t])}isCheck(){return this._isKingAttacked(this._turn)}inCheck(){return this.isCheck()}isCheckmate(){return this.isCheck()&&this._moves().length===0}isStalemate(){return!this.isCheck()&&this._moves().length===0}isInsufficientMaterial(){const t={b:0,n:0,r:0,q:0,k:0,p:0},s=[];let a=0,l=0;for(let u=ae.a8;u<=ae.h1;u++){if(l=(l+1)%2,u&136){u+=7;continue}const p=this._board[u];p&&(t[p.type]=p.type in t?t[p.type]+1:1,p.type===Dn&&s.push(l),a++)}if(a===2)return!0;if(a===3&&(t[Dn]===1||t[js]===1))return!0;if(a===t[Dn]+2){let u=0;const p=s.length;for(let g=0;g<p;g++)u+=s[g];if(u===0||u===p)return!0}return!1}isThreefoldRepetition(){return this._getPositionCount(this._hash)>=3}isDrawByFiftyMoves(){return this._halfMoves>=100}isDraw(){return this.isDrawByFiftyMoves()||this.isStalemate()||this.isInsufficientMaterial()||this.isThreefoldRepetition()}isGameOver(){return this.isCheckmate()||this.isDraw()}moves({verbose:t=!1,square:s=void 0,piece:a=void 0}={}){const l=this._moves({square:s,piece:a});return t?l.map(u=>new wn(this,u)):l.map(u=>this._moveToSan(u,l))}_moves({legal:t=!0,piece:s=void 0,square:a=void 0}={}){var A;const l=a?a.toLowerCase():void 0,u=s==null?void 0:s.toLowerCase(),p=[],g=this._turn,b=Rr(g);let S=ae.a8,E=ae.h1,N=!1;if(l)if(l in ae)S=E=ae[l],N=!0;else return[];for(let x=S;x<=E;x++){if(x&136){x+=7;continue}if(!this._board[x]||this._board[x].color===b)continue;const{type:j}=this._board[x];let W;if(j===Be){if(u&&u!==j)continue;W=x+ms[g][0],this._board[W]||(Ut(p,g,x,W,Be),W=x+ms[g][1],n1[g]===Zt(x)&&!this._board[W]&&Ut(p,g,x,W,Be,void 0,le.BIG_PAWN));for(let U=2;U<4;U++)W=x+ms[g][U],!(W&136)&&(((A=this._board[W])==null?void 0:A.color)===b?Ut(p,g,x,W,Be,this._board[W].type,le.CAPTURE):W===this._epSquare&&Ut(p,g,x,W,Be,Be,le.EP_CAPTURE))}else{if(u&&u!==j)continue;for(let U=0,q=Da[j].length;U<q;U++){const K=Da[j][U];for(W=x;W+=K,!(W&136);){if(!this._board[W])Ut(p,g,x,W,j);else{if(this._board[W].color===g)break;Ut(p,g,x,W,j,this._board[W].type,le.CAPTURE);break}if(j===js||j===Ke)break}}}}if((u===void 0||u===Ke)&&(!N||E===this._kings[g])){if(this._castling[g]&le.KSIDE_CASTLE){const x=this._kings[g],j=x+2;!this._board[x+1]&&!this._board[j]&&!this._attacked(b,this._kings[g])&&!this._attacked(b,x+1)&&!this._attacked(b,j)&&Ut(p,g,this._kings[g],j,Ke,void 0,le.KSIDE_CASTLE)}if(this._castling[g]&le.QSIDE_CASTLE){const x=this._kings[g],j=x-2;!this._board[x-1]&&!this._board[x-2]&&!this._board[x-3]&&!this._attacked(b,this._kings[g])&&!this._attacked(b,x-1)&&!this._attacked(b,j)&&Ut(p,g,this._kings[g],j,Ke,void 0,le.QSIDE_CASTLE)}}if(!t||this._kings[g]===-1)return p;const _=[];for(let x=0,j=p.length;x<j;x++)this._makeMove(p[x]),this._isKingAttacked(g)||_.push(p[x]),this._undoMove();return _}move(t,{strict:s=!1}={}){let a=null;if(typeof t=="string")a=this._moveFromSan(t,s);else if(t===null)a=this._moveFromSan(bs,s);else if(typeof t=="object"){const u=this._moves();for(let p=0,g=u.length;p<g;p++)if(t.from===ze(u[p].from)&&t.to===ze(u[p].to)&&(!("promotion"in u[p])||t.promotion===u[p].promotion)){a=u[p];break}}if(!a)throw typeof t=="string"?new Error(`Invalid move: ${t}`):new Error(`Invalid move: ${JSON.stringify(t)}`);if(this.isCheck()&&a.flags&le.NULL_MOVE)throw new Error("Null move not allowed when in check");const l=new wn(this,a);return this._makeMove(a),this._incPositionCount(),l}_push(t){this._history.push({move:t,kings:{b:this._kings.b,w:this._kings.w},turn:this._turn,castling:{b:this._castling.b,w:this._castling.w},epSquare:this._epSquare,halfMoves:this._halfMoves,moveNumber:this._moveNumber})}_movePiece(t,s){this._hash^=this._pieceKey(t),this._board[s]=this._board[t],delete this._board[t],this._hash^=this._pieceKey(s)}_makeMove(t){var l,u,p,g;const s=this._turn,a=Rr(s);if(this._push(t),t.flags&le.NULL_MOVE){s===ut&&this._moveNumber++,this._halfMoves++,this._turn=a,this._epSquare=Ge;return}if(this._hash^=this._epKey(),this._hash^=this._castlingKey(),t.captured&&(this._hash^=this._pieceKey(t.to)),this._movePiece(t.from,t.to),t.flags&le.EP_CAPTURE&&(this._turn===ut?this._clear(t.to-16):this._clear(t.to+16)),t.promotion&&(this._clear(t.to),this._set(t.to,{type:t.promotion,color:s})),this._board[t.to].type===Ke){if(this._kings[s]=t.to,t.flags&le.KSIDE_CASTLE){const b=t.to-1,S=t.to+1;this._movePiece(S,b)}else if(t.flags&le.QSIDE_CASTLE){const b=t.to+1,S=t.to-2;this._movePiece(S,b)}this._castling[s]=0}if(this._castling[s]){for(let b=0,S=Bt[s].length;b<S;b++)if(t.from===Bt[s][b].square&&this._castling[s]&Bt[s][b].flag){this._castling[s]^=Bt[s][b].flag;break}}if(this._castling[a]){for(let b=0,S=Bt[a].length;b<S;b++)if(t.to===Bt[a][b].square&&this._castling[a]&Bt[a][b].flag){this._castling[a]^=Bt[a][b].flag;break}}if(this._hash^=this._castlingKey(),t.flags&le.BIG_PAWN){let b;s===ut?b=t.to-16:b=t.to+16,!(t.to-1&136)&&((l=this._board[t.to-1])==null?void 0:l.type)===Be&&((u=this._board[t.to-1])==null?void 0:u.color)===a||!(t.to+1&136)&&((p=this._board[t.to+1])==null?void 0:p.type)===Be&&((g=this._board[t.to+1])==null?void 0:g.color)===a?(this._epSquare=b,this._hash^=this._epKey()):this._epSquare=Ge}else this._epSquare=Ge;t.piece===Be?this._halfMoves=0:t.flags&(le.CAPTURE|le.EP_CAPTURE)?this._halfMoves=0:this._halfMoves++,s===ut&&this._moveNumber++,this._turn=a,this._hash^=ps}undo(){const t=this._hash,s=this._undoMove();if(s){const a=new wn(this,s);return this._decPositionCount(t),a}return null}_undoMove(){const t=this._history.pop();if(t===void 0)return null;this._hash^=this._epKey(),this._hash^=this._castlingKey();const s=t.move;this._kings=t.kings,this._turn=t.turn,this._castling=t.castling,this._epSquare=t.epSquare,this._halfMoves=t.halfMoves,this._moveNumber=t.moveNumber,this._hash^=this._epKey(),this._hash^=this._castlingKey(),this._hash^=ps;const a=this._turn,l=Rr(a);if(s.flags&le.NULL_MOVE)return s;if(this._movePiece(s.to,s.from),s.piece&&(this._clear(s.from),this._set(s.from,{type:s.piece,color:a})),s.captured)if(s.flags&le.EP_CAPTURE){let u;a===ut?u=s.to-16:u=s.to+16,this._set(u,{type:Be,color:l})}else this._set(s.to,{type:s.captured,color:l});if(s.flags&(le.KSIDE_CASTLE|le.QSIDE_CASTLE)){let u,p;s.flags&le.KSIDE_CASTLE?(u=s.to+1,p=s.to-1):(u=s.to-2,p=s.to+1),this._movePiece(p,u)}return s}pgn({newline:t=` `,maxWidth:s=0}={}){const a=[];let l=!1;for(const _ in this._header)this._header[_]&&a.push(`[${_} "${this._header[_]}"]`+t),l=!0;l&&this._history.length&&a.push(t);const u=_=>{const A=this._comments[this.fen()];if(typeof A<"u"){const x=_.length>0?" ":"";_=`${_}${x}{${A}}`}return _},p=[];for(;this._history.length>0;)p.push(this._undoMove());const g=[];let b="";for(p.length===0&&g.push(u(""));p.length>0;){b=u(b);const _=p.pop();if(!_)break;if(!this._history.length&&_.color==="b"){const A=`${this._moveNumber}. ...`;b=b?`${b} ${A}`:A}else _.color==="w"&&(b.length&&g.push(b),b=this._moveNumber+".");b=b+" "+this._moveToSan(_,this._moves({legal:!0})),this._makeMove(_)}if(b.length&&g.push(u(b)),g.push(this._header.Result||"*"),s===0)return a.join("")+g.join(" ");const S=function(){return a.length>0&&a[a.length-1]===" "?(a.pop(),!0):!1},E=function(_,A){for(const x of A.split(" "))if(x){if(_+x.length>s){for(;S();)_--;a.push(t),_=0}a.push(x),_+=x.length,a.push(" "),_++}return S()&&_--,_};let N=0;for(let _=0;_<g.length;_++){if(N+g[_].length>s&&g[_].includes("{")){N=E(N,g[_]);continue}N+g[_].length>s&&_!==0?(a[a.length-1]===" "&&a.pop(),a.push(t),N=0):_!==0&&(a.push(" "),N++),a.push(g[_]),N+=g[_].length}return a.join("")}header(...t){for(let s=0;s<t.length;s+=2)typeof t[s]=="string"&&typeof t[s+1]=="string"&&(this._header[t[s]]=t[s+1]);return this._header}setHeader(t,s){return this._header[t]=s??As[t]??null,this.getHeaders()}removeHeader(t){return t in this._header?(this._header[t]=As[t]||null,!0):!1}getHeaders(){const t={};for(const[s,a]of Object.entries(this._header))a!==null&&(t[s]=a);return t}loadPgn(t,{strict:s=!1,newlineChar:a=`\r? `}={}){a!==`\r? `&&(t=t.replace(new RegExp(a,"g"),` `));const l=Wv(t);this.reset();const u=l.headers;let p="";for(const S in u)S.toLowerCase()==="fen"&&(p=u[S]),this.header(S,u[S]);if(!s)p&&this.load(p,{preserveHeaders:!0});else if(u.SetUp==="1"){if(!("FEN"in u))throw new Error("Invalid PGN: FEN tag must be sup