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

// Core imports
import { isPlatformBrowser } from '@angular/common'
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core'

// Services
import { SendCartService } from '../../services/svc.send-cart'

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

export class AddressComponent {

    // ---- Variables ---- \\
    timeout: any = []
    secondaryAddress: boolean = false
    session: string = undefined

    constructor(
        @Inject(PLATFORM_ID) private _platformId,
        private _sendCart: SendCartService
    ) { }


    // ---- Lifecycle hooks ---- \\
    ngOnInit() {



        if(isPlatformBrowser(this._platformId)) {
            const _session = this._sendCart.sessionStore('get')
            this.session = _session['customer'] ? _session['customer'] : undefined

            this.secondaryAddress = this._sendCart.sessionStore('get').checkSecondaryAddress
        }
    }


    // ---- Functions ---- \\
    getResult(event, id) {

        if(!!this.timeout[id])
            clearTimeout(this.timeout[id])
        
        this.timeout[id] = setTimeout(() => {
            this._sendCart.setAddress(id, event)
        }, 500)
    }

    onCheck() {
        this.secondaryAddress = !this.secondaryAddress

        this._sendCart.sessionStore('set', 'checkSecondaryAddress', this.secondaryAddress)
    }

}
