"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChildToManyParentsRelation = void 0;
const dirty_value_calculator_1 = __importDefault(require("../../../dirty-value-calculator"));
const guard_1 = __importDefault(require("../../../guard"));
const entity_load_configuration_1 = require("../../querying/entity-load-configuration");
const relation_1 = require("../relation");
const relation_role_1 = require("../relation-role");
class ChildToManyParentsRelation extends relation_1.ChildRelationBase {
    get isFullyLoaded() {
        return this.parentTotal == null || this._parents.length === this.parentTotal;
    }
    get isDirty() {
        if (!this.isTracking) {
            return false;
        }
        else {
            return this._dirtyValueCalculator.isDirty(this._parents);
        }
    }
    get parents() {
        return this._parents.slice();
    }
    constructor(name, properties = null, client, parentTotal) {
        super(name, properties, client);
        this._parents = [];
        this.role = relation_role_1.RelationRole.Child;
        this.isMultiValue = true;
        this.parentTotal = parentTotal;
    }
    getIds() {
        return this._parents.slice();
    }
    setIds(ids) {
        guard_1.default.validIds(ids);
        this._parents.splice(0, this._parents.length);
        this._parents.push(...ids);
    }
    clear() {
        this._parents.splice(0, this._parents.length);
    }
    add(id) {
        guard_1.default.validId(id);
        if (!this._parents.includes(id)) {
            this._parents.push(id);
        }
    }
    addRange(ids) {
        guard_1.default.validIds(ids);
        ids.forEach(id => {
            if (!this._parents.includes(id)) {
                this._parents.push(id);
            }
        });
    }
    addAsync(identifier) {
        return __awaiter(this, void 0, void 0, function* () {
            guard_1.default.notNullOrUndefined(this.client);
            guard_1.default.stringNotNullOrEmpty(identifier);
            const entity = yield this.client.entities.getAsync(identifier, entity_load_configuration_1.EntityLoadConfiguration.Minimal);
            if (entity && entity.id) {
                this.add(entity.id);
            }
        });
    }
    addRangeAsync(identifiers) {
        return __awaiter(this, void 0, void 0, function* () {
            guard_1.default.notNullOrUndefined(this.client);
            guard_1.default.arrayNoneNullOrEmptyString(identifiers);
            const entities = yield this.client.entities.getManyAsync(identifiers, entity_load_configuration_1.EntityLoadConfiguration.Minimal);
            if (entities) {
                this.addRange(entities.filter(x => x.id !== null).map(x => x.id));
            }
        });
    }
    startTracking() {
        if (this.isTracking)
            return;
        this.isTracking = true;
        this._dirtyValueCalculator = new dirty_value_calculator_1.default();
        this._dirtyValueCalculator.setOriginalValue(this._parents);
    }
    markClean() {
        if (this.isTracking) {
            this._dirtyValueCalculator.setOriginalValue(this._parents);
        }
    }
    getParentProperties(id) {
        return this.properties[id] == null ? {} : this.properties[id];
    }
}
exports.ChildToManyParentsRelation = ChildToManyParentsRelation;
//# sourceMappingURL=child-to-many-parents-relation.js.map