import { __decorate, __metadata } from "tslib";
import { Injector, Input } from '@angular/core';
import { DebugData, defineAssignedProp, defineSkipInitProp, PureRelationsComponent, RelationsChangeDetector, RelationsComponentManager } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { isPresent, nameof } from '@jscrpt/common';
import { NEVER } from 'rxjs';
import { DeconstructRelationsMetadataLoader } from './deconstruct.metadata';
/**
 * Deconstruct relations component
 */
let DeconstructRelations = class DeconstructRelations {
    //######################### public properties - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    get relationsOptions() {
        return this.ɵRelationsOptions;
    }
    set relationsOptions(value) {
        this.ɵRelationsOptions = value;
        this.initialize();
    }
    //######################### constructor #########################
    constructor(injector) {
        this.relationsChangeDetector = injector.get(RelationsChangeDetector);
        this.relationsComponentManager = injector.get(RelationsComponentManager);
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    ngOnChanges(changes) {
        if (nameof('object') in changes) {
            if (this.relationsOptions?.properties.length) {
                const id = this.relationsComponentManager.getId(this);
                for (const property of this.relationsOptions.properties) {
                    const thisDictionary = this;
                    thisDictionary[property.name] = this.object?.[property.name];
                    if (isPresent(id)) {
                        this.relationsChangeDetector.markForCheck({
                            componentId: id,
                            outputName: property.name,
                        });
                    }
                }
            }
        }
    }
    //######################### public properties - dynamic outputs #########################
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    //######################### protected methods #########################
    /**
     * Initialize rest relations
     */
    initialize() {
        if (this.relationsOptions) {
            if (this.relationsOptions.properties.length) {
                for (const property of this.relationsOptions.properties) {
                    if (property.name) {
                        Object.defineProperty(this, property.name, {
                            get: function () {
                                return this[`ɵ${property.name}`];
                            },
                            set: function (value) {
                                this[`ɵ${property.name}`] = value;
                                defineAssignedProp(this, property.name);
                            }
                        });
                        Object.defineProperty(this, `${property.name}Change`, {
                            value: NEVER,
                        });
                        if (property.skipInit) {
                            defineSkipInitProp(this, property.name);
                        }
                    }
                }
            }
        }
    }
};
__decorate([
    Input(),
    __metadata("design:type", Object)
], DeconstructRelations.prototype, "object", void 0);
DeconstructRelations = __decorate([
    DebugData({
        inputs: [
            nameof('object'),
        ],
        dynamicEndpointsGetter: function (obj) {
            if (!obj.ɵRelationsOptions) {
                return {
                    inputs: [],
                    outputs: [],
                };
            }
            return {
                inputs: [],
                outputs: obj.ɵRelationsOptions.properties?.map(itm => itm.name) ?? [],
            };
        }
    }),
    PureRelationsComponent(),
    RelationsEditorMetadata(DeconstructRelationsMetadataLoader),
    __metadata("design:paramtypes", [Injector])
], DeconstructRelations);
export { DeconstructRelations };
//# sourceMappingURL=deconstruct.relations.js.map