/**
 * Made by mkcomponent.sh
 * * Change to Fileheader info *
*/

import { isPlatformBrowser } from '@angular/common'
import { Component, OnInit, OnChanges, Inject, Output, EventEmitter, PLATFORM_ID } from '@angular/core'
import { SessionstorageService } from '@k-core/services/general/storage'
import { store } from '@k-settings/app-setup.js'
import { getStoresService } from '../../services/svc.getStores'


interface List {
    list: [
        {
            value: string;
            label: string;
        }
    ]
}

@Component({
    moduleId: module.id+ '',
    selector: 'storeDropdown-list',
    templateUrl: './template/t--list.pug',
    styleUrls: ['sty.list.scss']
})

export class ListComponent {

    // ---- Variables ---- \\
    list: any[]
    visible: boolean = false
    selectedValue: string = undefined
    store = store

    sessionId: string = 'decoRetailer'

    @Output() output = new EventEmitter
    constructor(
        @Inject(PLATFORM_ID) private _platformId,
        public sessionStorage: SessionstorageService,
        private _getService: getStoresService
    ) {

        _getService.getList().then((response: List) => {
            this.list = response.list
            if(!!this.list)
                this.output.emit(this.list[0].label)
        })
    }


    // ---- Lifecycle hooks ---- \\
    ngOnInit() {
        if(isPlatformBrowser(this._platformId)) {
            this.selectedValue = this.sessionStorage.getItem(this.sessionId)

            if(this.selectedValue) {
                this.visible = true
            }
        }
    }

    // ---- Functions ---- \\
    toggleVisible() {
        this.visible = !this.visible
    }

    setOption(event) {

        this.sessionStorage.setItem(this.sessionId, event)

        this.selectedValue = event
        this.output.emit(event)
    }
}
