import * as ILog from "@terrencecrowley/logabstract";

import * as OT from "./ottypes";
import * as OTC from "./otcomposite";

export interface IOTEngine
{
  cid(): string;
  rid(): string;

  toValue(): any;

  startLocalEdit(): OTC.OTCompositeResource;
  addLocalEdit(e: OTC.OTCompositeResource): void;
}

export class OTEngine implements IOTEngine
{
  ilog: ILog.ILog;
  onList: any;

	// Constructor
	constructor(ilog: ILog.ILog)
		{
      this.ilog = ilog;
      this.onList = {};
		}

  on(eventName: string, cb: any): void
    {
      let aCB: any = this.onList[eventName];
      if (aCB === undefined)
      {
        aCB = [];
        this.onList[eventName] = aCB;
      }
      aCB.push(cb);
    }

  off(eventName: string, cb: any): void
    {
      let aCB: any = this.onList[eventName];
      if (aCB !== undefined)
      {
        for (let i: number = 0; i < aCB.length; i++)
        {
          if (aCB[i] === cb)
          {
            aCB.splice(i, 1);
            break;
          }
        }
      }
    }

  emit(eventName: string): void
    {
      let aCB: any[] = this.onList[eventName];
      if (aCB !== undefined)
        for (let i: number = 0; i < aCB.length; i++)
          (aCB[i])();
    }

  cid(): string { return ''; }
  rid(): string { return ''; }

  toValue(): any { return null; }

  startLocalEdit(): OTC.OTCompositeResource { return null; }
  addLocalEdit(e: OTC.OTCompositeResource): void { }
}
