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

import { Observable } from 'rxjs';

import * as block from '../store/actions/block.actions';

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

import * as fromBlockReducer from '../store/reducers/block.reducer';

import * as fromBlockSelectors from '../store/selectors/block.selectors';

@Injectable()
export class BlockStoreService {

    constructor(private store$: Store<fromBlockReducer.State>) {
    }

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

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

    removeBlock(id: string): void {
        this.store$.dispatch(new block.RemoveBlock({ id }));
    }
}
