import { ChangeDetectionStrategy, Component, Input, OnInit, OnDestroy } from '@angular/core';

import { Observable } from 'rxjs';

import { BlockComponent } from '@cc/shared/block';

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

import { BlockStoreService } from './block-store.service';

@Component({
    selector: '@module.scoe-name@-ct',
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: [
        BlockStoreService,
    ],
    template: `
        <@module.scope-name@-cp
            [block]="block$ | async">
        </@module.scope-name@-cp>`,
})
export class @module.class.name@ContainerComponent implements BlockComponent, OnInit, OnDestroy {
    @Input() readonly block: Block;

    block$: Observable<Block>;

    constructor(private blockStore: BlockStoreService) {
    }

    ngOnInit(): void {
        this.asyncPipeObs();

        this.blockStore.upsertBlock(this.block);
    }

    ngOnDestroy(): void {
    }

    private asyncPipeObs(): void {
        this.block$ = this.blockStore.getBlockById(this.block.id);
    }
}
