import { QueueItemModel } from 'src/modules/queue/data/models/queue.model';
import { CustomerQueueManager } from './customer-queue.manager';

export class CustomerQueueListManager extends CustomerQueueManager {
  get data() {
    const tickets = this.tickets;
    const ticketItems = {};

    tickets.forEach((ticket) => {
      ticket.items.forEach((item) => {
        const item_id = item.item.item_queue?.id ?? item.item.id;
        const currentItem = ticketItems[item_id];
        item.ticket = ticket;
        ticketItems[item_id] = currentItem ? [...currentItem, item] : [item];
      });
    });

    return Object.values<QueueItemModel[]>(ticketItems).map((items) => {
      const item = items[0];
      const item_qty = items.reduce((acc, item) => acc + item.qty, 0);
      return {
        id: item.item_id,
        title: item.item.item_queue?.name ?? item.item.name,
        image_url: item.item.image_url,
        qty: item_qty,

        items: items.map((item) => {
          return {
            code: item.ticket.code,
            customer: item.ticket.customer,
            phone: item.ticket.phone,
            id: item.ticket.id,
            queue: item.queue.map((q) => {
              return {
                code: q.code,
                qty: q.qty,
                time: this.toTime(q.time),
                status: q.status,
              };
            }),
          };
        }),
      };
    });
  }
}
