import { __awaiter } from "tslib";
import { Directive, Inject, Input, Optional, ViewContainerRef } from '@angular/core';
import { LOGGER } from '@anglr/common';
import { LAYOUT_EDITOR_PROPERTY_TYPE_CONTROLS } from '../../../../misc/tokens';
import * as i0 from "@angular/core";
/**
 * Directive used for rendering property type control
 */
export class PropertyTypeControlRendererDirective {
    //######################### constructor #########################
    constructor(_viewContainerRef, _typeControls, _logger) {
        this._viewContainerRef = _viewContainerRef;
        this._typeControls = _typeControls;
        this._logger = _logger;
        //######################### protected fields #########################
        /**
         * Created component reference
         */
        this._componentRef = null;
        /**
         * Array of available values
         */
        this.values = [];
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    ngOnChanges() {
        var _a, _b, _c;
        return __awaiter(this, void 0, void 0, function* () {
            const typeName = (_a = this.typeName) !== null && _a !== void 0 ? _a : 'inputString';
            (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug('PropertyTypeControlRendererDirective: rendering property type control {{@type}}', { type: typeName });
            this.ngOnDestroy();
            this._viewContainerRef.clear();
            // metadata are present
            if (this.typeName) {
                const injector = this._viewContainerRef.injector;
                const type = this._typeControls[typeName];
                if (!type) {
                    (_c = this._logger) === null || _c === void 0 ? void 0 : _c.error('PropertyTypeControlRendererDirective: unable to find property type control {{@type}}', { type: typeName });
                    throw new Error(`unable to find property type control ${typeName}`);
                }
                this._componentRef = this._viewContainerRef.createComponent(type, {
                    injector,
                });
                if (this._componentRef) {
                    const component = this._componentRef.instance;
                    component.control = this.control;
                    component.values = this.values;
                    yield component.initialize();
                    component.invalidateVisuals();
                }
            }
        });
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        var _a, _b, _c;
        if (this._componentRef) {
            (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug('PropertyTypeControlRendererDirective: destroying property type control {{@type}}', { type: (_b = this.typeName) !== null && _b !== void 0 ? _b : 'inputString' });
            (_c = this._componentRef) === null || _c === void 0 ? void 0 : _c.destroy();
            this._componentRef = null;
        }
    }
}
PropertyTypeControlRendererDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: PropertyTypeControlRendererDirective, deps: [{ token: i0.ViewContainerRef }, { token: LAYOUT_EDITOR_PROPERTY_TYPE_CONTROLS }, { token: LOGGER, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
PropertyTypeControlRendererDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.4", type: PropertyTypeControlRendererDirective, selector: "[propertyTypeControl]", inputs: { control: "control", typeName: ["propertyTypeControl", "typeName"], values: "values" }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: PropertyTypeControlRendererDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[propertyTypeControl]'
                }]
        }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LAYOUT_EDITOR_PROPERTY_TYPE_CONTROLS]
                }] }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LOGGER]
                }, {
                    type: Optional
                }] }], propDecorators: { control: [{
                type: Input
            }], typeName: [{
                type: Input,
                args: ['propertyTypeControl']
            }], values: [{
                type: Input
            }] } });
//# sourceMappingURL=propertyTypeControlRenderer.directive.js.map