import { Injectable, inject } from '@angular/core';
import { Store, select } from '@ngrx/store';

import { Observable } from 'rxjs';

import { Block } from '../models';

import { actions } from '../store/block.actions';
import { feature } from '../store/block.feature';

@Injectable()
export class BlockStoreService {
    private store$ = inject(Store);

    getBlockById(id: string): Observable<Block> {
        return this.store$.pipe(select(feature.getBlock(id)));
    }

    upsertBlock(b: Block): void {
        this.store$.dispatch(actions.upsertBlock({ block: b }));
    }

    removeBlock(id: string): void {
        this.store$.dispatch(actions.removeBlock({ id }));
    }
}
