import { Component, OnInit, OnChanges, Input, ViewEncapsulation } from '@angular/core'

import { StaticBlockService }	from '../services/svc.static-block'

@Component({
	selector: 'static-block',
	templateUrl: 'tpl.static-block.pug',
	styleUrls: ['sty.static-block.scss'],
	encapsulation: ViewEncapsulation.None
})
export class StaticBlockView implements OnInit {

	// ---- Variables ---- \\
	@Input('block-url') blockUrl
	@Input('block-type') blockType = 'article'
	staticBlock

	constructor(
		private _staticBlockService : StaticBlockService
	) { }


	// ---- Lifecycle hooks ---- \\
	ngOnInit() { 
		this.getGetBlock()
	}

	ngOnChanges() {
		this.getGetBlock()
	}


	// ---- Functions ---- \\

	/**
	 * Get the static block content from the service
	 * 
	 * TODO: create fail-state for response with no block data
	 */
	getGetBlock() {
		this.staticBlock = null

		if(!!this.blockUrl) {
			this._staticBlockService.getBlockContent(this.blockUrl)
			.subscribe({
				next: (response) => {
					
					this.staticBlock = response
				},
				error: (e) => {
					console.error(e)
				}
			})
		}
	}
}