var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { inject, bindable, child, children, Container, customElement, inlineView, TaskQueue, ViewResources } from "aurelia-framework";
import { ComponentUtil, Grid } from "ag-grid-community";
import { AureliaFrameworkFactory } from "./aureliaFrameworkFactory";
import { generateBindables } from "./agUtils";
import { AgFullWidthRowTemplate } from './agTemplate';
import { AureliaFrameworkComponentWrapper } from "./aureliaFrameworkComponentWrapper";
export var AgGridAurelia = (function () {
    function AgGridAurelia(element, taskQueue, auFrameworkFactory, container, viewResources, aureliaFrameworkComponentWrapper) {
        var _this = this;
        this.taskQueue = taskQueue;
        this.auFrameworkFactory = auFrameworkFactory;
        this.container = container;
        this.viewResources = viewResources;
        this.aureliaFrameworkComponentWrapper = aureliaFrameworkComponentWrapper;
        this._initialised = false;
        this._destroyed = false;
        this.columns = [];
        this._nativeElement = element;
        ComponentUtil.EVENTS.forEach(function (eventName) {
            _this[eventName] = function () {
            };
        });
    }
    AgGridAurelia.prototype.attached = function () {
        this.taskQueue.queueTask(this.initGrid.bind(this));
    };
    AgGridAurelia.prototype.initGrid = function () {
        this._initialised = false;
        this._destroyed = false;
        this.auFrameworkFactory.setContainer(this.container);
        this.auFrameworkFactory.setViewResources(this.viewResources);
        this.aureliaFrameworkComponentWrapper.setContainer(this.container);
        this.aureliaFrameworkComponentWrapper.setViewResources(this.viewResources);
        this.gridOptions = ComponentUtil.copyAttributesToGridOptions(this.gridOptions, this);
        this.gridParams = {
            globalEventListener: this.globalEventListener.bind(this),
            frameworkFactory: this.auFrameworkFactory,
            providedBeanInstances: {
                frameworkComponentWrapper: this.aureliaFrameworkComponentWrapper
            }
        };
        if (this.columns && this.columns.length > 0) {
            this.gridOptions.columnDefs = this.columns
                .map(function (column) {
                return column.toColDef();
            });
        }
        if (this.fullWidthRowTemplate) {
            this.gridOptions.fullWidthCellRenderer =
                { template: this.fullWidthRowTemplate.template };
        }
        new Grid(this._nativeElement, this.gridOptions, this.gridParams);
        this.api = this.gridOptions.api;
        this.columnApi = this.gridOptions.columnApi;
        this._initialised = true;
    };
    AgGridAurelia.prototype.propertyChanged = function (propertyName, newValue, oldValue) {
        var changes = {};
        changes[propertyName] = { currentValue: newValue, previousValue: oldValue };
        if (this._initialised) {
            ComponentUtil.processOnChange(changes, this.gridOptions, this.api, this.columnApi);
        }
    };
    AgGridAurelia.prototype.detached = function () {
        if (this._initialised) {
            this._destroyed = true;
            this.api.destroy();
        }
    };
    AgGridAurelia.prototype.globalEventListener = function (eventType, event) {
        if (this._destroyed) {
            return;
        }
        var emitter = this[eventType];
        if (emitter) {
            emitter(event);
        }
        else {
            console.log('ag-Grid-aurelia: could not find EventEmitter: ' + eventType);
        }
    };
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], AgGridAurelia.prototype, "gridOptions", void 0);
    __decorate([
        bindable(),
        __metadata("design:type", Object)
    ], AgGridAurelia.prototype, "context", void 0);
    __decorate([
        children('ag-grid-column'),
        __metadata("design:type", Array)
    ], AgGridAurelia.prototype, "columns", void 0);
    __decorate([
        child('ag-full-width-row-template'),
        __metadata("design:type", AgFullWidthRowTemplate)
    ], AgGridAurelia.prototype, "fullWidthRowTemplate", void 0);
    AgGridAurelia = __decorate([
        customElement('ag-grid-aurelia'),
        generateBindables(ComponentUtil.ALL_PROPERTIES.filter(function (property) { return property !== 'gridOptions'; })),
        generateBindables(ComponentUtil.EVENTS),
        inlineView("<template><slot></slot></template>"),
        inject(Element, TaskQueue, AureliaFrameworkFactory, Container, ViewResources, AureliaFrameworkComponentWrapper),
        __metadata("design:paramtypes", [Element,
            TaskQueue,
            AureliaFrameworkFactory,
            Container,
            ViewResources,
            AureliaFrameworkComponentWrapper])
    ], AgGridAurelia);
    return AgGridAurelia;
}());

//# sourceMappingURL=agGridAurelia.js.map
