/*
	Author: Jeremy Espinosa
	Filename: handlebar.js
	Description: 
	Dependencies: 
	How to use: 
*/

;(function($) {
    'use strict'

    B2C.handlebar = {
        init: function(option) {
            this._default = {
                target: '#js-hb-template',
                container: '.js-hb-container',
                wrapper: '.js-hb-wrapper',
                data: {}
            }

            this._option = $.extend(true, this._default, option)

            if (!$(this._option.target).length) return

            this.registerHelper()
            this.renderTemplate()
        },

        registerHelper: function() {
            // compare condition
            /* sample code:
                {{#compare 1 2 operator="=="}}
                    Equal!
                {{/else}}
                    Not Equal!
                {{/compare}}
            */
            Handlebars.registerHelper('compare', function(
                lvalue,
                rvalue,
                options
            ) {
                if (arguments.length < 3)
                    throw new Error(
                        "Handlerbars Helper 'compare' needs 2 parameters"
                    )

                let operator = options.hash.operator || '==',
                    operators = {
                        '==': function(l, r) {
                            return l == r
                        },
                        '===': function(l, r) {
                            return l === r
                        },
                        '!=': function(l, r) {
                            return l != r
                        },
                        '<': function(l, r) {
                            return l < r
                        },
                        '>': function(l, r) {
                            return l > r
                        },
                        '<=': function(l, r) {
                            return l <= r
                        },
                        '>=': function(l, r) {
                            return l >= r
                        },
                        typeof: function(l, r) {
                            return typeof l == r
                        }
                    }

                if (!operators[operator])
                    throw new Error(
                        "Handlerbars Helper 'compare' doesn't know the operator " +
                            operator
                    )

                let result = operators[operator](lvalue, rvalue)

                if (result) return options.fn(this)
                return options.inverse(this)
            })

            // check if the integer is even or not
            Handlebars.registerHelper('isEven', function(conditional, options) {
                if (conditional % 2 == 0) return options.fn(this)
                return options.inverse(this)
            })
        },

        renderTemplate: function() {
            let template = $(this._option.target).html(),
                $container = $(this._option.container),
                compiledTemplate = Handlebars.compile(template),
                html = compiledTemplate(this._option.data)

            if ($container.find(this._option.wrapper).length) {
                $container
                    .find(this._option.wrapper)
                    .remove()
                    .promise()
                    .done(function() {
                        $container.append(html)
                    })
            } else {
                $container.append(html)
            }
        }
    }
})(jQuery)
