/**
 * @since Tue Aug 07 2018
 * @author Zia ur-Rehman - Klean
 */

import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core'
import { FooterTemplate } from '@k-core/modules/footer/template/cmp.footer'
import { DomSanitizer, makeStateKey, TransferState } from '@angular/platform-browser'


import { website } from '@k-settings/app-setup'
import { FooterService } from '@k-core/modules/footer/services/svc.footer'
import { MenuService } from '@k-core/services/svc.menu'
import { HelperService } from '@k-core/services/svc.helper'
import { isPlatformBrowser } from '@angular/common'

// Keys for TransferState data
const SITEINFO = makeStateKey('siteInfoKey')
const FOOTERMENU = makeStateKey('footerMenuKey')

@Component({
    moduleId: module.id+ '',
    selector: 'footer-kid',
    templateUrl: './tpl.footer.kid.pug',
    styleUrls: ['./sty.footer.kid.scss'],

})

export class FooterKidTemplate extends FooterTemplate implements OnInit {
    website: string = website
    siteInfo: any
    footerMenu: any

    constructor(
        _sanitizer: DomSanitizer,
        _footerService: FooterService,
        _menuService: MenuService,
        _helper: HelperService,
        state: TransferState,
        @Inject(PLATFORM_ID) private _platformId
    ) {
        super(_sanitizer, _footerService, _menuService, _helper, state)
    }


    ngOnInit() {

        if(isPlatformBrowser(this._platformId)) {

            // pulls and adds SiteInfo to TransferState
            this.siteInfo = this.state.get(SITEINFO, null as any)
    
            if(!this.siteInfo) {
    
                this._helper.siteData$.subscribe((response: any) => {
                    this.siteInfo = response.siteInfo
                    this.state.set(SITEINFO, response.siteInfo as any)
                })
            }
    
            // Pulls and adds footer-menu to TransferState
            this.footerMenu = this.state.get(FOOTERMENU, null as any)
            
            if(!this.footerMenu) {
    
                this._menuService.getMenu('footer-menu').then((response) => {
                    this.footerMenu = response
                    this.state.set(FOOTERMENU, response as any)
                })
            }
        }
    }


    /**
     * strips spaces from a string, returns it without spaces
     * 
     * @param {string} phone
     * @returns {string}
     */
    stripNumber(phone: string): string {

        let result = ''

        for(let number of phone.split(' ')) {
            result += number
        }

        return result
    }
}