System.register(["@angular/core", "@angular/material/dialog", "./tree.component", "./tree-dialog.component", "../confirm-dialog/confirm-dialog.component", "lodash", "@stratusjs/runtime/stratus", "@stratusjs/core/environment", "../core/responsive.component"], function (exports_1, context_1) {
    "use strict";
    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);
    };
    var core_1, dialog_1, tree_component_1, tree_dialog_component_1, confirm_dialog_component_1, lodash_1, stratus_1, environment_1, responsive_component_1, systemDir, moduleName, parentModuleName, min, localDir, TreeNodeComponent;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (dialog_1_1) {
                dialog_1 = dialog_1_1;
            },
            function (tree_component_1_1) {
                tree_component_1 = tree_component_1_1;
            },
            function (tree_dialog_component_1_1) {
                tree_dialog_component_1 = tree_dialog_component_1_1;
            },
            function (confirm_dialog_component_1_1) {
                confirm_dialog_component_1 = confirm_dialog_component_1_1;
            },
            function (lodash_1_1) {
                lodash_1 = lodash_1_1;
            },
            function (stratus_1_1) {
                stratus_1 = stratus_1_1;
            },
            function (environment_1_1) {
                environment_1 = environment_1_1;
            },
            function (responsive_component_1_1) {
                responsive_component_1 = responsive_component_1_1;
            }
        ],
        execute: function () {
            systemDir = '@stratusjs/angular';
            moduleName = 'tree-node';
            parentModuleName = 'tree';
            min = !environment_1.cookie('env') ? '.min' : '';
            localDir = `${stratus_1.Stratus.BaseUrl}${boot.configuration.paths[`${systemDir}/*`].replace(/[^/]*$/, '').replace(/\/dist\/$/, '/src/')}`;
            TreeNodeComponent = class TreeNodeComponent extends responsive_component_1.ResponsiveComponent {
                constructor(dialog, ref) {
                    super();
                    this.dialog = dialog;
                    this.ref = ref;
                    this.title = moduleName + '_component';
                    this.isInitialized = false;
                    this.isDestroyed = false;
                    this.isStyled = false;
                    this._ = lodash_1.default;
                    this.Stratus = stratus_1.Stratus;
                    this.hasChild = (node) => node.children && node.children.length > 0;
                    this.isExpanded = (node) => node.meta ? node.meta.expanded : true;
                }
                ngOnInit() {
                    this.uid = lodash_1.default.uniqueId(`sa_${lodash_1.default.snakeCase(moduleName)}_component_`);
                    stratus_1.Stratus.Instances[this.uid] = this;
                    stratus_1.Stratus.Internals.CssLoader(`${localDir}/${parentModuleName}/${moduleName}.component${min}.css`)
                        .then(() => {
                        this.isStyled = true;
                        this.refresh();
                    })
                        .catch((err) => {
                        console.warn('Issue detected in CSS Loader for Component:', this);
                        console.error(err);
                        this.isStyled = true;
                        this.refresh();
                    });
                    this.node.meta.component = this;
                    this.isInitialized = true;
                    this.refresh();
                    this.onPostPersist();
                }
                ngOnDestroy() {
                    this.node.meta.component = null;
                    this.isDestroyed = true;
                }
                refresh() {
                    if (this.isDestroyed) {
                        return new Promise(resolve => resolve());
                    }
                    return super.refresh();
                }
                onPostPersist() {
                    if (!this.node) {
                        return;
                    }
                    if (!this.node.model) {
                        return;
                    }
                    if (!this.node.model.meta.get('postPersist')) {
                        return;
                    }
                    this.node.model.meta.set('postPersist', false);
                    this.openDialog();
                }
                destroy() {
                    const dialog = this.dialog
                        .open(confirm_dialog_component_1.ConfirmDialogComponent, {
                        maxWidth: '400px',
                        data: {
                            title: 'Delete MenuLink',
                            message: 'Are you sure you want to do this?'
                        }
                    });
                    dialog
                        .afterClosed()
                        .subscribe((dialogResult) => {
                        if (!dialogResult) {
                            return;
                        }
                        if (!this.node || !this.node.model) {
                            return;
                        }
                        this.node.model.destroy();
                    });
                    return dialog;
                }
                getName() {
                    if (!this.node.model || !this.node.model.get || !this.node.model.get('name')) {
                        return 'Untitled';
                    }
                    return this.node.model.get('name');
                }
                getDragPreview() {
                    return `name: ${this.getName()}<br>children: ${this.node.children ? this.node.children.length : 0}`;
                }
                toggleExpanded() {
                    if (!this.node.meta) {
                        console.error('node meta not found:', this.node);
                        return;
                    }
                    this.node.meta.expanded = !this.node.meta.expanded;
                    this.refresh();
                }
                toggleExpandedClick() {
                    this.isSingleClick = true;
                    setTimeout(() => {
                        if (!this.isSingleClick) {
                            return;
                        }
                        this.toggleExpanded();
                    }, this.tree.dblClickWait);
                }
                toggleExpandedDblClick() {
                    this.isSingleClick = false;
                    this.toggleExpanded();
                }
                openDialog() {
                    if (!this.node.model || !lodash_1.default.has(this.node.model, 'data')) {
                        return;
                    }
                    const dialogRef = this.dialog.open(tree_dialog_component_1.TreeDialogComponent, {
                        width: '400px',
                        autoFocus: true,
                        disableClose: true,
                        restoreFocus: false,
                        data: {
                            tree: this.tree,
                            treeNode: this,
                            backend: this.tree.backend,
                            iconRegistry: this.tree.iconRegistry,
                            id: this.node.model.data.id || null,
                            name: this.node.model.data.name || '',
                            target: this.node.model.data.url ? 'url' : 'content',
                            content: this.node.model.data.content || null,
                            url: this.node.model.data.url || null,
                            browserTarget: this.node.model.data.browserTarget || null,
                            model: this.node.model || null,
                            collection: this.tree.collection || null,
                            parent: this.node.model.data.parent || null,
                            nestParent: this.node.model.data.nestParent || null,
                        }
                    });
                    this.refresh();
                    const that = this;
                    dialogRef.afterClosed().subscribe((result) => {
                        if (!result || lodash_1.default.isEmpty(result)) {
                            return;
                        }
                        this.tree.unsettled = true;
                        const attrs = [
                            'name',
                            'content',
                            'url',
                            'priority',
                            'browserTarget'
                        ];
                        attrs.forEach(attr => {
                            if (!lodash_1.default.has(result, attr)) {
                                return;
                            }
                            if ('content' === attr) {
                                const value = lodash_1.default.get(result, attr);
                                this.node.model.set(attr, !value ? null : { id: lodash_1.default.get(value, 'id') });
                                return;
                            }
                            this.node.model.set(attr, lodash_1.default.get(result, attr));
                        });
                        that.refresh();
                        that.tree.refresh();
                        this.node.model.save();
                        this.tree.unsettled = false;
                    });
                }
                openDialogClick() {
                    this.isSingleClick = true;
                    setTimeout(() => {
                        if (!this.isSingleClick) {
                            return;
                        }
                        this.openDialog();
                    }, this.tree.dblClickWait);
                }
                openDialogDblClick() {
                    this.isSingleClick = false;
                    this.openDialog();
                }
                getSvg(url, options) {
                    return this.tree.getSvg(url, options);
                }
                toggleStatus() {
                    if (!this.node || !this.node.model) {
                        return;
                    }
                    const model = this.node.model;
                    model.set('status', model.get('status') ? 0 : 1);
                    model.save();
                }
                addChild() {
                    let priority = lodash_1.default.max(this.tree.collection.models.filter((model) => {
                        const nestParent = model.get('nestParent');
                        if (!nestParent) {
                            return false;
                        }
                        return nestParent.id === this.node.id;
                    }).map((model) => model.get('priority'))) || -1;
                    if (typeof priority === 'number') {
                        priority++;
                    }
                    this.tree.collection.add({
                        name: 'Untitled Child',
                        parent: this.node.model.data.parent,
                        nestParent: {
                            id: this.node.model.data.id
                        },
                        priority
                    }, {
                        save: true,
                        trigger: true
                    });
                }
            };
            __decorate([
                core_1.Input(),
                __metadata("design:type", tree_component_1.TreeComponent)
            ], TreeNodeComponent.prototype, "tree", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Object)
            ], TreeNodeComponent.prototype, "parent", void 0);
            __decorate([
                core_1.Input(),
                __metadata("design:type", Object)
            ], TreeNodeComponent.prototype, "node", void 0);
            TreeNodeComponent = __decorate([
                core_1.Component({
                    selector: `sa-${moduleName}`,
                    templateUrl: `${localDir}/${parentModuleName}/${moduleName}.component${min}.html`,
                }),
                __metadata("design:paramtypes", [dialog_1.MatDialog,
                    core_1.ChangeDetectorRef])
            ], TreeNodeComponent);
            exports_1("TreeNodeComponent", TreeNodeComponent);
        }
    };
});

//# sourceMappingURL=tree-node.component.js.map
