import { Injectable } from '@angular/core';
import { IOnlineBoardRoutePageUrlParams } from '@online-board/services/url/types';
import { IScheduleRouteParams } from '@schedule/services/url/types';
import { IParsedFlightId } from '@typings/flight/flight-id';

export type ISearchHistoryItemParams =
    | IOnlineBoardRoutePageUrlParams
    | IParsedFlightId
    | IScheduleRouteParams;

export type ISearchHistoryItemType =
    | 'board-route'
    | 'schedule-route'
    | 'flight-number';

export type ISearchHistoryItem = {
    type: ISearchHistoryItemType;
    url: string;
    params: ISearchHistoryItemParams;
};

@Injectable({
    providedIn: 'root',
})
export class SearchHistoryService {
    private _items: ISearchHistoryItem[] = [];

    get items() {
        return this._items;
    }

    add(item: ISearchHistoryItem) {
        if (item.url === this.firstItem?.url) {
            return;
        }

        this._items = [item].concat(this._items);
    }

    private get firstItem() {
        return this._items[0];
    }
}
