import {Component} from '@angular/core';
import {Account, List} from 'projects/mastodon-api/src/public-api';
import {Observable} from "rxjs";
import {select, Store} from "@ngrx/store";
import {fromListViewPage, selectFilteredFollowingsWithLists, selectLists} from "../../shared/state/store";
import {MultiSelect} from "primeng/multiselect";

@Component({
    selector: 'app-list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.scss']
})
export class ListComponent {
    followings$: Observable<ReadonlyArray<Account>>;
    lists$: Observable<List[]>;

    constructor(private store: Store) {
        this.followings$ = this.store.pipe(select(selectFilteredFollowingsWithLists));
        this.lists$ = this.store.pipe(select(selectLists));
    }

    addAccountToSelectedList(accountId: string, select: MultiSelect) {
        select.value.forEach((listId: string) => {
            this.store.dispatch(fromListViewPage.addAccountToList({accountId, listId}));
        });
    }

    removeAccountFromList(accountId: string, listId: string) {
        this.store.dispatch(fromListViewPage.removeAccountFromList({accountId, listId}));
    }

}
