import { Collection, ICollection } from "../../Collection";
import { Order } from "./Order";

export interface IOrderCollection extends ICollection<Order> {
  findByField(field: string): Order | undefined;
  removeByField(field: string): void;
  toObject(): Record<string, any>;
}

export class OrderCollection
  extends Collection<Order>
  implements IOrderCollection
{
  public static createEmpty() {
    return new OrderCollection();
  }

  public static create(initialValues: Order[], totalCount?: number) {
    return new OrderCollection(initialValues, totalCount);
  }

  public findByField(field: string): Order | undefined {
    return this.find((order) => order.field === field);
  }

  public removeByField(field: string): void {
    let indexFound = -1;
    this.find((order, index) =>
      order.field === field ? (indexFound = index) : null,
    );
    if (indexFound > -1) {
      this.removeByIndex(indexFound);
    }
  }

  public toObject(): Record<string, any> {
    return this.items.map((order: Order) => order.toObject());
  }
}
