/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/cm-chess@3.6.3/src/Chess.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{Pgn}from"cm-pgn/src/Pgn.js";import{TAGS}from"cm-pgn/src/Header.js";import{Chess as ChessJs,SQUARES}from"chess.mjs/src/Chess.js";export const PIECES={p:{name:"pawn",value:1},n:{name:"knight",value:3},b:{name:"bishop",value:3},r:{name:"rook",value:5},q:{name:"queen",value:9},k:{name:"king",value:1/0}};export const COLOR={white:"w",black:"b"};export const FEN={empty:"8/8/8/8/8/8/8/8 w - - 0 1",start:"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"};export const EVENT_TYPE={illegalMove:"illegalMove",legalMove:"legalMove",undoMove:"undoMove",initialized:"initialized"};export const GAME_VARIANT={standard:"standard",chess960:"chess960"};function publishEvent(s,e){for(const t of s)setTimeout((()=>{t(e)}))}export class Chess{constructor(s={}){this.observers=[],this.props={fen:void 0,pgn:void 0,gameVariant:GAME_VARIANT.standard,chess960:void 0,sloppy:!0,...s},void 0!==this.props.chess960&&(console.warn("props.chess960 is deprecated, use GAME_VARIANT"),this.props.gameVariant=GAME_VARIANT.chess960),"string"==typeof s&&(console.warn('directly passing a FEN is deprecated, use `{fen: "'+s+'"}`'),this.props.fen=s),this.props.fen||this.props.pgn||(this.props.fen=FEN.start),this.props.fen?this.load(this.props.fen):this.props.pgn?this.loadPgn(this.props.pgn):this.load(FEN.start)}fen(s=this.lastMove()){return s?s.fen:this.setUpFen()}setUpFen(){return this.pgn.header.tags[TAGS.SetUp]?this.pgn.header.tags[TAGS.FEN]:FEN.start}header(){return this.pgn.header.tags}gameOver(s=this.lastMove()){return s?s.gameOver:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).game_over()}inDraw(s=this.lastMove()){return s?!0===s.inDraw:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).in_draw()}inStalemate(s=this.lastMove()){return s?!0===s.inStalemate:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).in_stalemate()}insufficientMaterial(s=this.lastMove()){return s?!0===s.insufficientMaterial:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).insufficient_material()}inThreefoldRepetition(s=this.lastMove()){return s&&!0===s.inThreefoldRepetition}inCheckmate(s=this.lastMove()){return s?!0===s.inCheckmate:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).in_checkmate()}inCheck(s=this.lastMove()){return s?!0===s.inCheck:new ChessJs(this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).in_check()}history(){return this.pgn.history.moves}lastMove(){return this.pgn.history.moves.length>0?this.pgn.history.moves[this.pgn.history.moves.length-1]:null}load(s){const e=new ChessJs(s,{chess960:this.props.gameVariant===GAME_VARIANT.chess960});if(!e||e.fen()!==s)throw Error("Invalid fen "+s);this.pgn=new Pgn(void 0,{chess960:this.props.gameVariant===GAME_VARIANT.chess960}),s!==FEN.start&&(this.pgn.header.tags[TAGS.SetUp]="1",this.pgn.header.tags[TAGS.FEN]=e.fen(),this.pgn.history.props.setUpFen=s),publishEvent(this.observers,{type:EVENT_TYPE.initialized,fen:s})}loadPgn(s,e=this.props.sloppy){this.pgn=new Pgn(s,{sloppy:e}),this.pgn.props.chess960&&(this.props.gameVariant=GAME_VARIANT.chess960),publishEvent(this.observers,{type:EVENT_TYPE.initialized,pgn:s})}move(s,e=void 0,t=this.props.sloppy){try{const i=this.pgn.history.addMove(s,e,t);return publishEvent(this.observers,{type:EVENT_TYPE.legalMove,move:i,previousMove:e}),i}catch(t){return publishEvent(this.observers,{type:EVENT_TYPE.illegalMove,move:s,previousMove:e}),null}}moves(s=void 0,e=this.lastMove()){return new ChessJs(this.fen(e),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).moves(s)}validateMove(s,e=void 0,t=this.props.sloppy){return this.pgn.history.validateMove(s,e,t)}renderPgn(s=!0,e=!0,t=!0){return this.pgn.render(s,e,t)}pieces(s=void 0,e=void 0,t=this.lastMove()){const i=new ChessJs(t?t.fen:this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960});let n=[];for(let t=0;t<64;t++){const r=SQUARES[t],h=i.get(r);h&&(h.square=r),s?(!e&&h&&h.type===s||h&&h.color===e&&h.type===s)&&n.push(h):!e&&h&&n.push(h)}return n}piece(s,e=this.lastMove()){return new ChessJs(e?e.fen:this.fen(),{chess960:this.props.gameVariant===GAME_VARIANT.chess960}).get(s)}turn(s=this.lastMove()){let e=0;if(this.setUpFen()){this.setUpFen().split(" ")[1]===COLOR.black&&(e=1)}return(s?s.ply:0)%2===e?COLOR.white:COLOR.black}undo(s=this.lastMove()){s.previous&&(s.previous.next=void 0);const e=s.variation.findIndex((e=>e.ply===s.ply));s.variation=s.variation.splice(e),publishEvent(this.observers,{type:EVENT_TYPE.undoMove,move:s})}plyCount(){return this.history().length}fenOfPly(s){return s>0?this.history()[s-1].fen:this.setUpFen()}addObserver(s){this.observers.push(s)}}
//# sourceMappingURL=/sm/d7c38a7c80640b41e0294b850d170ed340801a603516fc2801bb1f1f7ed5fff0.map