"use strict";
/**
 * Copyright (c) Haiku 2016-2018. All rights reserved.
 */
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var StringUtils_1 = require("../../helpers/StringUtils");
// tslint:disable-next-line:function-name
function HaikuAngularDOMAdapter(selector, haikuComponentFactory, optionalRawBytecode) {
    var HaikuAngularComponentInternal = /** @class */ (function () {
        function HaikuAngularComponentInternal() {
            var _this = this;
            this.componentWillInitialize = new core_1.EventEmitter();
            this.componentDidInitialize = new core_1.EventEmitter();
            this.componentDidMount = new core_1.EventEmitter();
            this.componentWillUnmount = new core_1.EventEmitter();
            this.automount = true;
            this.autoplay = true;
            this.forceFlush = true;
            this.freeze = true;
            this.loop = false;
            this.clock = {};
            this.alwaysComputeSizing = false;
            this.haikuConfig = {
                onComponentWillInitialize: function (component) { return _this.componentWillInitialize.emit(component); },
                onComponentDidInitialize: function (component) { return _this.componentDidInitialize.emit(component); },
                onComponentDidMount: function (component) { return _this.componentDidMount.emit(component); },
                onComponentWillUnmount: function (component) { return _this.componentWillUnmount.emit(component); },
            };
            this.randomId = "haiku-angularroot-" + StringUtils_1.randomString(24);
        }
        HaikuAngularComponentInternal.prototype.ngOnChanges = function (changes) {
            this.haikuConfig = Object.keys(changes).reduce(function (accumulator, configKey) {
                accumulator[configKey] = changes[configKey].currentValue;
                return accumulator;
            }, this.haikuConfig);
            if (this.haiku) {
                this.haiku.assignConfig(this.haikuConfig);
            }
        };
        HaikuAngularComponentInternal.prototype.ngAfterViewInit = function () {
            var haikuAdapter;
            if (this.haikuAdapter) {
                if (this.haikuCode) {
                    haikuAdapter = this.haikuAdapter(this.haikuCode);
                }
                else if (optionalRawBytecode) {
                    haikuAdapter = this.haikuAdapter(optionalRawBytecode);
                }
                else {
                    throw new Error('A Haiku code object is required if you supply a Haiku adapter');
                }
            }
            else {
                // Otherwise default to the adapter which was initialized in the wrapper module
                haikuAdapter = haikuComponentFactory;
            }
            if (!haikuAdapter) {
                throw new Error('A Haiku adapter is required');
            }
            // Reuse existing mounted component if one exists
            if (!this.haiku) {
                this.haiku = haikuAdapter(// eslint-disable-line
                this.mount.nativeElement, this.haikuConfig);
            }
            else {
                // If the component already exists, update its options and make sure it remounts.
                // This action is important if we are in e.g. React Router.
                //
                // Important: Note that we should NOT call remount if we just initialized the instance (i.e. stanza above)
                // because we'll end up pausing the timelines before the first mount, resulting in a blank context.
                this.haiku.callRemount(this.haikuConfig);
            }
        };
        HaikuAngularComponentInternal.prototype.ngOnDestroy = function () {
            if (this.haiku) {
                this.haiku.callUnmount();
            }
        };
        HaikuAngularComponentInternal.isHaikuAdapter = true;
        __decorate([
            core_1.ViewChild('mount'),
            __metadata("design:type", core_1.ElementRef)
        ], HaikuAngularComponentInternal.prototype, "mount", void 0);
        __decorate([
            core_1.Output(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "componentWillInitialize", void 0);
        __decorate([
            core_1.Output(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "componentDidInitialize", void 0);
        __decorate([
            core_1.Output(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "componentDidMount", void 0);
        __decorate([
            core_1.Output(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "componentWillUnmount", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "seed", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Number)
        ], HaikuAngularComponentInternal.prototype, "timestamp", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "automount", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "autoplay", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "forceFlush", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "freeze", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "loop", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Function)
        ], HaikuAngularComponentInternal.prototype, "frame", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "clock", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "sizing", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "alwaysComputeSizing", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "preserve3d", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "contextMenu", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "position", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "overflowX", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "overflowY", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "overflow", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "mixpanel", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Boolean)
        ], HaikuAngularComponentInternal.prototype, "useWebkitPrefix", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", String)
        ], HaikuAngularComponentInternal.prototype, "interactionMode", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "states", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "eventHandlers", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "timelines", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "vanities", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "placeholder", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "haikuAdapter", void 0);
        __decorate([
            core_1.Input(),
            __metadata("design:type", Object)
        ], HaikuAngularComponentInternal.prototype, "haikuCode", void 0);
        HaikuAngularComponentInternal = __decorate([
            core_1.Component({
                selector: selector,
                template: "<div #mount class=\"haiku-angular-wrapper\" [id]=\"randomId\"></div>",
                styles: ["\n      div {\n        position: relative;\n        margin: 0;\n        padding: 0;\n        border: 0;\n        width: 100%;\n        height: 100%;\n      }\n    "],
            })
        ], HaikuAngularComponentInternal);
        return HaikuAngularComponentInternal;
    }());
    var HaikuModule = /** @class */ (function () {
        function HaikuModule() {
        }
        HaikuModule = __decorate([
            core_1.NgModule({
                declarations: [HaikuAngularComponentInternal],
                exports: [HaikuAngularComponentInternal],
            })
        ], HaikuModule);
        return HaikuModule;
    }());
    return {
        ngModule: HaikuModule,
    };
}
exports.default = HaikuAngularDOMAdapter;
//# sourceMappingURL=HaikuAngularDOMAdapter.js.map