import init, { CoreApp } from "kifu-wasm";
import { CoreResponse, CoreRequest, PlayingFieldView } from "core-types";

export class CoreApi {
  core: CoreApp;

  constructor() {
    let app = new CoreApp();
    this.core = app;
  }

  async dispatch(request: CoreRequest): Promise<CoreResponse> {
    return await this.core.dispatch(request);
  }

  async playingField(): Promise<PlayingFieldView> {
    return (await this.dispatch({ type: "PlayingField" })).content;
  }
}

export const initCore = async (): Promise<CoreApi> => {
  await init();
  return new CoreApi();
};
