"use strict";
var angular = require("angular");
var _ = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var jquery_service_1 = require("../../services/jquery/jquery.service");
var templateLoader_service_1 = require("../../services/templateLoader/templateLoader.service");
exports.moduleName = 'rl.ui.components.genericContainer';
exports.componentName = 'rlGenericContainer';
exports.controllerName = 'GenericContainerController';
var GenericContainerController = (function () {
    function GenericContainerController($element, $transclude, $compile, object, jquery, templateLoader) {
        this.$element = $element;
        this.$transclude = $transclude;
        this.$compile = $compile;
        this.object = object;
        this.jquery = jquery;
        this.templateLoader = templateLoader;
    }
    GenericContainerController.prototype.$onChanges = function (changes) {
        if (this.container && changes.selector) {
            var template = this.resolveTemplate(changes.selector.currentValue);
            this.swapTemplates(template);
        }
    };
    GenericContainerController.prototype.refresh = function () {
        var template = this.resolveTemplate(this.selector);
        this.swapTemplates(template);
    };
    GenericContainerController.prototype.resolveTemplate = function (type) {
        if (_.has(this.templates, type)) {
            return this.templates[type];
        }
        else {
            return this.default;
        }
    };
    GenericContainerController.prototype.$postLink = function () {
        this.initDefaults();
        this.container = this.$element.find('#container');
        var templateResult = this.templateLoader.loadTemplates(this.$transclude);
        this.templates = _.extend(this.templates, templateResult.templates);
        this.default = templateResult.default;
        this.templateScope = templateResult.transclusionScope;
        if (!this.default) {
            this.default = '<div></div>';
        }
        this.refresh();
    };
    GenericContainerController.prototype.initDefaults = function () {
        this.default = this.defaultTemplate;
        this.templates = this.configuredTemplates ? this.configuredTemplates : {};
    };
    GenericContainerController.prototype.swapTemplates = function (template) {
        var content = angular.element(template);
        this.jquery.replaceContent(this.container, content);
        this.$compile(content)(this.templateScope);
    };
    return GenericContainerController;
}());
GenericContainerController.$inject = ['$element',
    '$transclude',
    '$compile',
    typescript_angular_utilities_1.downgrade.objectServiceName,
    jquery_service_1.serviceName,
    templateLoader_service_1.serviceName];
exports.GenericContainerController = GenericContainerController;
var genericContainer = {
    template: '<div id="container"></div>',
    transclude: true,
    controller: exports.controllerName,
    controllerAs: 'genericContainer',
    bindings: {
        selector: '<',
        configuredTemplates: '<templates',
        defaultTemplate: '<',
    },
};
angular.module(exports.moduleName, [jquery_service_1.moduleName, typescript_angular_utilities_1.downgrade.moduleName, templateLoader_service_1.moduleName])
    .component(exports.componentName, genericContainer)
    .controller(exports.controllerName, GenericContainerController);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJpY0NvbnRhaW5lci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImdlbmVyaWNDb250YWluZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLGlDQUFtQztBQUNuQywwQkFBNEI7QUFFNUIsNkVBQW1FO0FBR25FLHVFQUk4QztBQUU5QywrRkFJOEQ7QUFJbkQsUUFBQSxVQUFVLEdBQVcsbUNBQW1DLENBQUM7QUFDekQsUUFBQSxhQUFhLEdBQVcsb0JBQW9CLENBQUM7QUFDN0MsUUFBQSxjQUFjLEdBQVcsNEJBQTRCLENBQUM7QUFPakU7SUFpQkMsb0NBQW9CLFFBQWtDLEVBQzNDLFdBQXdDLEVBQ3hDLFFBQWlDLEVBQ2pDLE1BQStCLEVBQy9CLE1BQXNCLEVBQ3RCLGNBQStCO1FBTHRCLGFBQVEsR0FBUixRQUFRLENBQTBCO1FBQzNDLGdCQUFXLEdBQVgsV0FBVyxDQUE2QjtRQUN4QyxhQUFRLEdBQVIsUUFBUSxDQUF5QjtRQUNqQyxXQUFNLEdBQU4sTUFBTSxDQUF5QjtRQUMvQixXQUFNLEdBQU4sTUFBTSxDQUFnQjtRQUN0QixtQkFBYyxHQUFkLGNBQWMsQ0FBaUI7SUFBSSxDQUFDO0lBRS9DLCtDQUFVLEdBQVYsVUFBVyxPQUFpQztRQUMzQyxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsU0FBUyxJQUFJLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDO1lBQ3hDLElBQ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