import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { TranslatorService } from '../../services/translator/translator.service';
import { ConnectorService } from '../../services/connector/connector.service';
import { Input } from '@angular/core';
import { SelectModel } from './model/select.model';
import { Selection } from './model/selection';

@Component({
    selector: 'options-select',
    templateUrl: './select.component.html',
    styleUrls: ['./select.component.scss']
})
export class SelectComponent implements OnInit {

    @Input() title: string;
    @Input() placeHolder: string;
    @Input() dataPath: string;
    @Output() changed = new EventEmitter();
    public options: SelectModel[] = [];
    /*
    hold the output selection value
    */
    @Input() value: Selection;
    constructor(
        public ts: TranslatorService,
        public cs: ConnectorService) {
    }

    ngOnInit() {
        this.cs.getLocalResrouce(this.dataPath).subscribe(data => {
            if (this.value.value == null ) {
                const defValue = data['default'];
                if (defValue) {
                    this.value.value = defValue;
                }
            }
            this.options = data['options'];
        });
    }

    public optionSelected() {
        this.changed.emit( this.value.value);
    }

}

