System.register(["@angular/core", "@angular/forms", "@angular/material/dialog", "@angular/material/snack-bar", "rxjs", "lodash", "@stratusjs/runtime/stratus", "@stratusjs/core/environment", "../core/responsive.component", "@angular/material/icon", "@angular/platform-browser"], 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, dialog_1, snack_bar_1, rxjs_1, lodash_1, stratus_1, environment_1, responsive_component_1, icon_1, platform_browser_1, systemDir, moduleName, parentModuleName, min, localDir, CitationDialogComponent;
    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 (dialog_1_1) {
                dialog_1 = dialog_1_1;
            },
            function (snack_bar_1_1) {
                snack_bar_1 = snack_bar_1_1;
            },
            function (rxjs_1_1) {
                rxjs_1 = rxjs_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;
            },
            function (icon_1_1) {
                icon_1 = icon_1_1;
            },
            function (platform_browser_1_1) {
                platform_browser_1 = platform_browser_1_1;
            }
        ],
        execute: function () {
            systemDir = '@stratusjs/angular';
            moduleName = 'citation-dialog';
            parentModuleName = 'editor';
            min = !environment_1.cookie('env') ? '.min' : '';
            localDir = `${stratus_1.Stratus.BaseUrl}${boot.configuration.paths[`${systemDir}/*`].replace(/[^/]*$/, '').replace(/\/dist\/$/, '/src/')}`;
            CitationDialogComponent = class CitationDialogComponent extends responsive_component_1.ResponsiveComponent {
                constructor(dialogRef, data, fb, snackBar, iconRegistry, sanitizer, ref) {
                    super();
                    this.dialogRef = dialogRef;
                    this.data = data;
                    this.fb = fb;
                    this.snackBar = snackBar;
                    this.iconRegistry = iconRegistry;
                    this.sanitizer = sanitizer;
                    this.ref = ref;
                    this.title = moduleName + '_component';
                    this.Stratus = stratus_1.Stratus;
                    this.popupTitle = 'Create New Citation';
                    this.isPopupLoading = true;
                    this.formDisabled = false;
                    this.newCitation = true;
                    this.svgIcons = {};
                }
                ngOnInit() {
                    this.uid = lodash_1.uniqueId(`sa_${lodash_1.snakeCase(moduleName)}_component_`);
                    stratus_1.Stratus.Instances[this.uid] = this;
                    stratus_1.Stratus.Internals.CssLoader(`${localDir}${parentModuleName}/${moduleName}.component${min}.css`);
                    this.model = this.data.model;
                    this.property = this.data.property;
                    this.editor = this.data.editor;
                    this.eventManager = this.data.eventManager;
                    this.baseEditor = this.data.eventManager.editor;
                    this.dialogCitationForm = this.fb.group({
                        citationContentInput: new forms_1.FormControl('', [forms_1.Validators.required]),
                        citationTitleInput: new forms_1.FormControl(''),
                    });
                    this.highlightedText = this.baseEditor.selection.text();
                    const containedElement = this.baseEditor.citationManager.getSelectedCitation() ||
                        this.baseEditor.citationManager.getLastKnownCitation();
                    this.baseEditor.citationManager.hideToolbar();
                    if (containedElement) {
                        this.newCitation = false;
                        this.existingCitationElement = containedElement;
                        const copiedElement = containedElement.cloneNode(true);
                        Array.from(copiedElement.getElementsByClassName('fr-marker')).forEach((markerEl) => {
                            markerEl.remove();
                        });
                        const contentElement = lodash_1.head(copiedElement.getElementsByTagName('stratus-citation-content'));
                        if (contentElement) {
                            this.dialogCitationForm.get('citationContentInput').setValue(contentElement.innerHTML);
                        }
                        const titleElement = lodash_1.head(copiedElement.getElementsByTagName('stratus-citation-title'));
                        if (titleElement) {
                            this.dialogCitationForm.get('citationTitleInput').setValue(titleElement.innerHTML);
                        }
                    }
                    if (this.newCitation && !lodash_1.isEmpty(this.highlightedText)) {
                        this.popupTitle = 'Convert Text into Citation';
                        this.dialogCitationForm.get('citationTitleInput').setValue(this.highlightedText);
                    }
                    if (!this.newCitation) {
                        this.popupTitle = 'Update Citation';
                    }
                    this.isPopupLoading = false;
                    this.refresh();
                }
                cancel() {
                    this.dialogRef.close();
                    this.refresh();
                }
                confirm() {
                    if (lodash_1.isUndefined(this.dialogCitationForm.get('citationContentInput'))) {
                        return;
                    }
                    const content = this.dialogCitationForm.get('citationContentInput').value;
                    if (lodash_1.isUndefined(content) || !lodash_1.isString(content)) {
                        return;
                    }
                    const title = this.dialogCitationForm.get('citationTitleInput').value;
                    const citationElement = this.createCitationHTML(content, title);
                    if (!citationElement) {
                        console.warn(`${moduleName}: unable to build citation for: ${content}`);
                        this.snackBar.open(`Unable to build citation for: ${content}.`, 'dismiss', {
                            duration: 5000,
                            horizontalPosition: 'right',
                            verticalPosition: 'bottom'
                        });
                        return;
                    }
                    if (!this.eventManager) {
                        console.warn(`${moduleName}: event manager is not set.`);
                        return;
                    }
                    if (!this.newCitation && this.existingCitationElement) {
                        this.existingCitationElement.outerHTML = citationElement;
                    }
                    else {
                        this.eventManager.trigger('insert', citationElement, this.editor);
                    }
                    this.dialogRef.close();
                }
                createCitationHTML(content, title) {
                    if (!content) {
                        console.warn(`${moduleName}: unable to create citation for empty content`);
                        return null;
                    }
                    if (!lodash_1.isEmpty(title)) {
                        title = `<stratus-citation-title>${title}</stratus-citation-title>`;
                    }
                    else {
                        title = `<stratus-citation-notitle/>`;
                    }
                    return `<stratus-citation><stratus-citation-content>${content}</stratus-citation-content>${title}</stratus-citation-content>`;
                }
                getSvg(url, options) {
                    const uid = this.addSvgIcon(url, options);
                    return new rxjs_1.Observable((subscriber) => {
                        this.iconRegistry
                            .getNamedSvgIcon(uid)
                            .subscribe({
                            error(err) {
                                console.error(`getSvg(${url}): ${err}`);
                            },
                            complete() {
                                subscriber.next(uid);
                            }
                        });
                    });
                }
                addSvgIcon(url, options) {
                    if (url in this.svgIcons) {
                        return this.svgIcons[url];
                    }
                    if (!options) {
                        options = {};
                    }
                    const uid = this.svgIcons[url] = lodash_1.uniqueId('selector_svg');
                    this.iconRegistry.addSvgIcon(uid, this.sanitizer.bypassSecurityTrustResourceUrl(url), options);
                    return uid;
                }
            };
            CitationDialogComponent = __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,
                    snack_bar_1.MatSnackBar,
                    icon_1.MatIconRegistry,
                    platform_browser_1.DomSanitizer,
                    core_1.ChangeDetectorRef])
            ], CitationDialogComponent);
            exports_1("CitationDialogComponent", CitationDialogComponent);
        }
    };
});

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