import { Directive, Inject, Input, Optional, ViewContainerRef } from '@angular/core';
import { LOGGER } from '@anglr/common';
import * as i0 from "@angular/core";
/**
 * Directive used for rendering properties control
 */
export class PropertiesControlRendererDirective {
    //######################### constructor #########################
    constructor(_viewContainerRef, _logger) {
        this._viewContainerRef = _viewContainerRef;
        this._logger = _logger;
        //######################### protected fields #########################
        /**
         * Created component reference
         */
        this._componentRef = null;
        /**
         * Properties metadata that are being rendered
         */
        this.propertiesMetadata = null;
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    async ngOnChanges() {
        this._logger?.debug('PropertiesControlRendererDirective: rendering properties control {{@type}}', { type: this.type?.name });
        this.ngOnDestroy();
        this._viewContainerRef.clear();
        // metadata are present
        if (this.type) {
            const injector = this._viewContainerRef.injector;
            this._componentRef = this._viewContainerRef.createComponent(this.type, {
                injector,
            });
            if (this._componentRef) {
                const component = this._componentRef.instance;
                component.propertiesMetadata = this.propertiesMetadata;
                component.form = this.form;
                await component.initialize();
                component.invalidateVisuals();
            }
        }
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        if (this._componentRef) {
            this._logger?.debug('PropertiesControlRendererDirective: destroying properties control {{@type}}', { type: this.type?.name });
            this._componentRef?.destroy();
            this._componentRef = null;
        }
    }
}
PropertiesControlRendererDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: PropertiesControlRendererDirective, deps: [{ token: i0.ViewContainerRef }, { token: LOGGER, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
PropertiesControlRendererDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.4", type: PropertiesControlRendererDirective, selector: "[propertiesControl]", inputs: { form: "form", propertiesMetadata: "propertiesMetadata", type: ["propertiesControl", "type"] }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: PropertiesControlRendererDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[propertiesControl]'
                }]
        }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LOGGER]
                }, {
                    type: Optional
                }] }], propDecorators: { form: [{
                type: Input
            }], propertiesMetadata: [{
                type: Input
            }], type: [{
                type: Input,
                args: ['propertiesControl']
            }] } });
//# sourceMappingURL=propertiesControlRenderer.directive.js.map