import { __decorate, __metadata } from "tslib";
import { Injector } from '@angular/core';
import { defineAssignedProp, defineSkipInitProp, PureRelationsComponent, RelationsComponentManager, RelationsProcessor } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { isPresent, nameof } from '@jscrpt/common';
import { NEVER } from 'rxjs';
import { SyncValuesRelationsMetadataLoader } from './syncValues.metadata';
/**
 * Sync values relations component
 */
let SyncValuesRelations = class SyncValuesRelations {
    //######################### public properties - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    get relationsOptions() {
        return this.ɵRelationsOptions;
    }
    set relationsOptions(value) {
        this.ɵRelationsOptions = value;
        this.initialize();
    }
    //######################### constructor #########################
    constructor(injector) {
        //######################### public properties - inputs #########################
        /**
         * Trigger object
         */
        this.trigger = {};
        this.relationsProcessor = injector.get(RelationsProcessor);
        this.relationsComponentManager = injector.get(RelationsComponentManager);
    }
    //######################### public methods - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    ngOnChanges(changes) {
        var _a;
        if (!this.relationsOptions) {
            return;
        }
        if (this.relationsOptions.triggerSync) {
            if (nameof('trigger') in changes) {
                this.sendData();
            }
        }
        else {
            if (this.timeout) {
                cancelIdleCallback(this.timeout);
            }
            this.timeout = requestIdleCallback(() => this.sendData(), { timeout: (_a = this.relationsOptions.idleTimeout) !== null && _a !== void 0 ? _a : 0 });
        }
    }
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    //######################### protected methods #########################
    /**
     * Initialize rest relations
     */
    initialize() {
        if (this.relationsOptions) {
            if (this.relationsOptions.syncProperties && Array.isArray(this.relationsOptions.syncProperties)) {
                for (const property of this.relationsOptions.syncProperties) {
                    Object.defineProperty(this, property, {
                        get: function () {
                            return this[`ɵ${property}`];
                        },
                        set: function (value) {
                            this[`ɵ${property}`] = value;
                            defineAssignedProp(this, property);
                        }
                    });
                    Object.defineProperty(this, `${property}Change`, {
                        value: NEVER,
                    });
                    //TODO: make this configurable
                    defineSkipInitProp(this, property);
                }
            }
        }
    }
    /**
     * Sends output data
     */
    sendData() {
        const id = this.relationsComponentManager.getId(this);
        if (isPresent(id)) {
            this.relationsProcessor.transferOutputsData(id, false, true);
        }
    }
};
SyncValuesRelations = __decorate([
    PureRelationsComponent(),
    RelationsEditorMetadata(SyncValuesRelationsMetadataLoader),
    __metadata("design:paramtypes", [Injector])
], SyncValuesRelations);
export { SyncValuesRelations };
//# sourceMappingURL=syncValues.relations.js.map