import { UniqueID } from "./value-objects";

export abstract class DomainEntity<T extends object> {
  protected readonly props: T;
  public readonly id: UniqueID;

  protected constructor(props: T, id?: UniqueID) {
    this.id = id ? id : UniqueID.generateNewID();
    this.props = props;
  }

  protected _flattenProps(props: T): { [s: string]: any } {
    return Object.entries(props).reduce((result: any, [key, valueObject]) => {
      console.log(key, valueObject.value);
      result[key] = valueObject.value;

      return result;
    }, {});
  }

  equals(other: DomainEntity<T>): boolean {
    return other instanceof DomainEntity && this.id.equals(other.id);
  }

  toString(): { [s: string]: string } {
    const flattenProps = this._flattenProps(this.props);

    return {
      id: this.id.toString(),
      ...flattenProps.map((prop: any) => String(prop)),
    };
  }
}
