System.register(["@angular/core", "@angular/forms", "@angular/cdk/drag-drop", "@angular/material/dialog", "rxjs/operators", "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 __param = (this && this.__param) || function (paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); }
    };
    var core_1, forms_1, drag_drop_1, dialog_1, operators_1, lodash_1, stratus_1, environment_1, responsive_component_1, systemDir, moduleName, parentModuleName, min, localDir, TreeDialogComponent;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (forms_1_1) {
                forms_1 = forms_1_1;
            },
            function (drag_drop_1_1) {
                drag_drop_1 = drag_drop_1_1;
            },
            function (dialog_1_1) {
                dialog_1 = dialog_1_1;
            },
            function (operators_1_1) {
                operators_1 = operators_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-dialog';
            parentModuleName = 'tree';
            min = !environment_1.cookie('env') ? '.min' : '';
            localDir = `${stratus_1.Stratus.BaseUrl}${boot.configuration.paths[`${systemDir}/*`].replace(/[^/]*$/, '').replace(/\/dist\/$/, '/src/')}`;
            TreeDialogComponent = class TreeDialogComponent extends responsive_component_1.ResponsiveComponent {
                constructor(dialogRef, data, fb, ref) {
                    super();
                    this.dialogRef = dialogRef;
                    this.data = data;
                    this.fb = fb;
                    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.apiBase = '/Api/Content';
                    this.isSingleContentLoading = false;
                    this.limit = 20;
                    this.isContentLoading = false;
                    this.isContentLoaded = false;
                    this.basicContentQueryAttributes = 'options[isContent]=null&options[isCollection]=null&options[showRoutable]=true&options[showRouting]=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;
                    })
                        .catch((err) => {
                        console.warn('Issue detected in CSS Loader for Component:', this);
                        console.error(err);
                        this.isStyled = true;
                    });
                    this.tree = this.data.tree;
                    this.treeNode = this.data.treeNode;
                    this.backend = this.data.backend;
                    this.iconRegistry = this.data.iconRegistry;
                    this.dialogContentForm = this.fb.group({
                        contentSelectorInput: this.data.content || null
                    });
                    this.dialogContentForm
                        .get('contentSelectorInput')
                        .valueChanges
                        .pipe(operators_1.debounceTime(300), operators_1.tap(() => this.isContentLoading = true), operators_1.switchMap((value) => {
                        if (lodash_1.default.isString(value)) {
                            this.lastContentSelectorQuery = this.getQueryUrl(value);
                        }
                        else {
                            this.data.content = value;
                            this.data.url = null;
                        }
                        return this.backend.get(this.lastContentSelectorQuery || this.getQueryUrl())
                            .pipe(operators_1.finalize(() => {
                            this.isContentLoading = false;
                            this.isContentLoaded = true;
                        }));
                    }))
                        .subscribe((response) => this.handleContent(response));
                    this.backend
                        .get(this.getQueryUrl())
                        .pipe(operators_1.finalize(() => this.isContentLoading = false))
                        .subscribe((response) => this.handleContent(response));
                    if (this.data.content && this.data.content.id) {
                        this.isSingleContentLoading = true;
                        this.backend
                            .get(this.getQueryUrl(null, this.data.content.id))
                            .pipe(operators_1.finalize(() => this.isSingleContentLoading = false))
                            .subscribe((response) => {
                            if (!response.ok || response.status !== 200 || lodash_1.default.isEmpty(response.body)) {
                                return null;
                            }
                            const payload = lodash_1.default.get(response.body, 'payload') || response.body;
                            if (lodash_1.default.isEmpty(payload) || Array.isArray(payload) || !lodash_1.default.isObject(payload)) {
                                return null;
                            }
                            this.contentEntity = payload;
                            this.isSingleContentLoading = false;
                            this.dialogContentForm
                                .get('contentSelectorInput')
                                .patchValue(this.contentEntity);
                            this.refresh();
                            return this.contentEntity;
                        });
                    }
                    this.isInitialized = true;
                }
                ngOnDestroy() {
                    this.isDestroyed = true;
                }
                refresh() {
                    if (this.isDestroyed) {
                        return new Promise(resolve => resolve());
                    }
                    return super.refresh();
                }
                onCancelClick() {
                    this.dialogRef.close();
                }
                onBrowserTargetChange($event) {
                    this.data.browserTarget = this.browserTarget ? '_blank' : null;
                }
                displayContentText(content) {
                    if (!content) {
                        return;
                    }
                    const routing = lodash_1.default.get(content, 'routing[0].url');
                    const routingText = !lodash_1.default.isUndefined(routing) ? ` (/${routing})` : ' (No route!)';
                    const contentId = lodash_1.default.get(content, 'id');
                    const contentIdText = !lodash_1.default.isUndefined(contentId) ? `Content: ${contentId}` : null;
                    return (lodash_1.default.get(content, 'version.title') || contentIdText) + routingText;
                }
                handleContent(response) {
                    if (!response.ok || response.status !== 200 || lodash_1.default.isEmpty(response.body)) {
                        this.filteredContent = [];
                        return this.filteredContent;
                    }
                    const payload = lodash_1.default.get(response.body, 'payload') || response.body;
                    if (lodash_1.default.isEmpty(payload) || !Array.isArray(payload)) {
                        this.filteredContent = [];
                        return this.filteredContent;
                    }
                    this.filteredContent = payload;
                    return this.filteredContent;
                }
                destroy() {
                    this.treeNode.destroy()
                        .afterClosed()
                        .subscribe((dialogResult) => {
                        if (!dialogResult) {
                            return;
                        }
                        this.onCancelClick();
                    });
                }
                toggleStatus() {
                    this.treeNode.toggleStatus();
                }
                addChild() {
                    this.treeNode.addChild();
                }
                isSelected(content) {
                    if (!this.data.content) {
                        return false;
                    }
                    return this.data.content.id === content.id;
                }
                getSvg(url, options) {
                    return this.tree.getSvg(url, options);
                }
                getQueryUrl(query, id) {
                    query = (!lodash_1.default.isString(query) || lodash_1.default.isEmpty(query)) ? '' : `"${query}"`;
                    id = !lodash_1.default.isString(id) && !lodash_1.default.isNumber(id) ? '' : `/${id}`;
                    return `${this.apiBase}${id}?limit=${this.limit}&${this.basicContentQueryAttributes}&q=${query}`;
                }
                selectedOnTop(list, selected) {
                    if (!list || !list.length) {
                        return list;
                    }
                    if (!selected || !selected.id) {
                        return list;
                    }
                    const orderedList = lodash_1.default.clone(list);
                    const index = list.findIndex((v) => v.id === selected.id);
                    if (index === -1) {
                        orderedList.unshift(selected);
                        return orderedList;
                    }
                    drag_drop_1.moveItemInArray(orderedList, index, 0);
                    return orderedList;
                }
            };
            TreeDialogComponent = __decorate([
                core_1.Component({
                    selector: `sa-${moduleName}`,
                    templateUrl: `${localDir}/${parentModuleName}/${moduleName}.component${min}.html`,
                }),
                __param(1, core_1.Inject(dialog_1.MAT_DIALOG_DATA)),
                __metadata("design:paramtypes", [dialog_1.MatDialogRef, Object, forms_1.FormBuilder,
                    core_1.ChangeDetectorRef])
            ], TreeDialogComponent);
            exports_1("TreeDialogComponent", TreeDialogComponent);
        }
    };
});

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