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

import {ComponentChanges, EndpointConfigService} from 'm2-core';
import {CD_PLUGIN_DOCUMENTATION_CTX} from '../../../config';


@Component({
    selector: '[cdApiLink]',
    template: `<a href="{{href}}" target="_blank"><ng-content></ng-content></a>`
})
export class CdApiLinkComponent implements OnChanges {

    constructor(
        private endpointConfigService: EndpointConfigService
    ) {
    }

    // tslint:disable-next-line:no-input-rename
    @Input('cdApiLink') path: string;

    href?: string;


    ngOnChanges(changes: ComponentChanges<CdApiLinkComponent>): void {
        if (changes.path) {
            this.href = this.endpointConfigService.baseUrl
                + CD_PLUGIN_DOCUMENTATION_CTX
                + this.path;
        }
    }

}
