import { __decorate, __metadata } from "tslib";
import { DebugData, DynamicOutput, PureRelationsComponent } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { nameof } from '@jscrpt/common';
import { FirstNonNullRelationsMetadataLoader } from './firstNonNull.metadata';
/**
 * First non null relations component
 */
let FirstNonNullRelations = class FirstNonNullRelations {
    constructor() {
        //######################### protected properties #########################
        /**
         * Dynamic data values to be checked
         */
        this.dynamicData = {};
    }
    //######################### public properties - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    get relationsOptions() {
        return this.ɵRelationsOptions;
    }
    set relationsOptions(value) {
        this.ɵRelationsOptions = value;
        this.initialize();
    }
    //######################### public methods - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    ngOnChanges(changes) {
        if (nameof('data1') in changes || nameof('data2') in changes) {
            this.evaluateResult();
        }
    }
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    /**
     * Initialize logical and relations
     */
    initialize() {
        var _a, _b;
        if (this.relationsOptions) {
            if ((_b = (_a = this.relationsOptions) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.length) {
                for (const name of this.relationsOptions.properties) {
                    if (name) {
                        Object.defineProperty(this, name, {
                            set: function (value) {
                                this.dynamicData[name] = value;
                                this.evaluateResult();
                            }
                        });
                    }
                }
            }
        }
    }
    /**
     * Evaluates conditions result
     */
    evaluateResult() {
        var _a, _b;
        if (this.data1) {
            this.data = this.data1;
            return;
        }
        if (this.data2) {
            this.data = this.data2;
            return;
        }
        if ((_b = (_a = this.relationsOptions) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.length) {
            for (const name of this.relationsOptions.properties) {
                if (this.dynamicData[name]) {
                    this.data = this.dynamicData[name];
                    return;
                }
            }
        }
    }
};
__decorate([
    DynamicOutput(),
    __metadata("design:type", Object)
], FirstNonNullRelations.prototype, "data", void 0);
FirstNonNullRelations = __decorate([
    DebugData({
        inputs: [
            nameof('data1'),
            nameof('data2'),
        ],
        outputs: [
            nameof('data'),
        ],
    }),
    PureRelationsComponent(),
    RelationsEditorMetadata(FirstNonNullRelationsMetadataLoader)
], FirstNonNullRelations);
export { FirstNonNullRelations };
//# sourceMappingURL=firstNonNull.relations.js.map