import { __awaiter } from "tslib";
import { Directive, ElementRef, HostListener, Injector, Input } from '@angular/core';
import { TitledDialogService } from '@anglr/common/material';
import { LayoutEditorPropertyMetadataExtractor } from '@anglr/dynamic/layout-editor';
import { lastValueFrom } from '@jscrpt/common/rxjs';
import { CustomComponentOptionsSAComponent } from '../../components';
import { getCustomComponentMeta } from '../../misc/utils';
import { CustomComponentsRegister } from '../../services';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/common/material";
import * as i2 from "../../services";
import * as i3 from "@anglr/dynamic/layout-editor";
/**
 * Directive that is used for displaying custom component options
 */
export class ShowCustomComponentOptionsSADirective {
    //######################### constructor #########################
    constructor(injector, dialogSvc, customComponentsRegister, propsMetadataExtractor, element) {
        this.injector = injector;
        this.dialogSvc = dialogSvc;
        this.customComponentsRegister = customComponentsRegister;
        this.propsMetadataExtractor = propsMetadataExtractor;
        this.element = element;
        //######################### protected properties #########################
        /**
         * Metadata for each component in custom component
         */
        this.customComponentContentMetadata = {};
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    ngOnChanges() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.name) {
                return;
            }
            const result = (yield getCustomComponentMeta(this.name, this.injector));
            if (!result) {
                return;
            }
            this.customComponentContentMetadata = result.contentMetadata;
            this.customComponentMetadata = result.metadata;
        });
    }
    //######################### public methods - implementation of OnInit #########################
    /**
     * Initialize component
     */
    ngOnInit() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.name && !this.element.nativeElement.disabled) {
                throw new Error('Please provide name of custom component for its options');
            }
        });
    }
    //######################### protected methods - host #########################
    /**
     * Shows content options selection dialog
     * @param event - Event that occured
     */
    show(event) {
        var _a, _b, _c;
        return __awaiter(this, void 0, void 0, function* () {
            event.preventDefault();
            event.stopPropagation();
            const result = yield lastValueFrom(this.dialogSvc.open(CustomComponentOptionsSAComponent, {
                title: 'custom component options',
                width: '75vw',
                data: {
                    customComponentContentMetadata: this.customComponentContentMetadata,
                    configuration: (_b = this.customComponentsRegister.getConfigurationForComponent((_a = this.name) !== null && _a !== void 0 ? _a : '')) !== null && _b !== void 0 ? _b : {},
                    propsMetadataExtractor: this.propsMetadataExtractor,
                }
            }).afterClosed());
            if (result) {
                yield this.customComponentsRegister.setConfigurationForComponent((_c = this.name) !== null && _c !== void 0 ? _c : '', result);
            }
        });
    }
}
ShowCustomComponentOptionsSADirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ShowCustomComponentOptionsSADirective, deps: [{ token: i0.Injector }, { token: i1.TitledDialogService }, { token: i2.CustomComponentsRegister }, { token: i3.LayoutEditorPropertyMetadataExtractor }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
ShowCustomComponentOptionsSADirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.4", type: ShowCustomComponentOptionsSADirective, isStandalone: true, selector: "[showCustomComponentOptions]", inputs: { name: ["showCustomComponentOptions", "name"] }, host: { listeners: { "click": "show($event)" } }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ShowCustomComponentOptionsSADirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[showCustomComponentOptions]',
                    standalone: true,
                }]
        }], ctorParameters: () => [{ type: i0.Injector }, { type: i1.TitledDialogService }, { type: i2.CustomComponentsRegister }, { type: i3.LayoutEditorPropertyMetadataExtractor }, { type: i0.ElementRef }], propDecorators: { name: [{
                type: Input,
                args: ['showCustomComponentOptions']
            }], show: [{
                type: HostListener,
                args: ['click', ['$event']]
            }] } });
//# sourceMappingURL=showCustomComponentOptions.directive.js.map