import { Component, Input, OnChanges }      from "@angular/core"
import { StorelocatorService }           from '../../../../services/svc.store-locator'
@Component({
    moduleId: module.id+ '',
    selector: 'stores-nearby',
    templateUrl: './tpl.stores-nearby.pug',
    styleUrls: ['./sty.stores-nearby.scss']
})

export class StorelocatorStoresNearbyComponent implements OnChanges {

    @Input('id') id: any;

    stores: any;
    types: any;

    filteredStores: Array<any> = [];

    constructor(
        private storelocatorService: StorelocatorService
    ) { }

    ngOnChanges() {

        this.storelocatorService.getTypes().then(response => {
            this.types = response
        })

        this.storelocatorService.getNearbyStores(this.id).then(response => {

            this.stores = response.stores
            this.filteredStores = []

            for(let store of this.stores) {
                if(!!store.type) {
                    if(!!!this.filteredStores[store.type]) {
                        this.filteredStores[store.type] = []
                    }

                    this.filteredStores[store.type].push(store)
                }
            }    
        })
    }
}