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());
    });
};
import DirtyValueCalculator from "../../../dirty-value-calculator";
import Guard from "../../../guard";
import { EntityLoadConfiguration } from "../../querying/entity-load-configuration";
import { RelationBase } from "../relation";
import { RelationRole } from "../relation-role";
export class ParentToManyChildrenRelation extends RelationBase {
    get isFullyLoaded() {
        return this.childTotal == null || this._children.length === this.childTotal;
    }
    get isDirty() {
        if (!this.isTracking) {
            return false;
        }
        else {
            return this._dirtyValueCalculator.isDirty(this._children);
        }
    }
    get children() {
        return this._children.slice();
    }
    constructor(name, properties = null, client, childTotal) {
        super(name, properties, client);
        this._children = [];
        this.role = RelationRole.Parent;
        this.isMultiValue = true;
        this.childTotal = childTotal;
    }
    getIds() {
        return this._children.slice();
    }
    setIds(ids) {
        Guard.validIds(ids);
        this._children.splice(0, this._children.length);
        this._children.push(...ids);
    }
    clear() {
        this._children.splice(0, this._children.length);
    }
    add(id) {
        Guard.validId(id);
        if (!this._children.includes(id)) {
            this._children.push(id);
        }
    }
    addRange(ids) {
        Guard.validIds(ids);
        ids.forEach(id => {
            if (!this._children.includes(id)) {
                this._children.push(id);
            }
        });
    }
    addAsync(identifier) {
        return __awaiter(this, void 0, void 0, function* () {
            Guard.notNullOrUndefined(this.client);
            Guard.stringNotNullOrEmpty(identifier);
            const entity = yield this.client.entities.getAsync(identifier, EntityLoadConfiguration.Minimal);
            if (entity && entity.id) {
                this.add(entity.id);
            }
        });
    }
    addRangeAsync(identifiers) {
        return __awaiter(this, void 0, void 0, function* () {
            Guard.notNullOrUndefined(this.client);
            Guard.arrayNoneNullOrEmptyString(identifiers);
            const entities = yield this.client.entities.getManyAsync(identifiers, 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 DirtyValueCalculator();
        this._dirtyValueCalculator.setOriginalValue(this._children);
    }
    markClean() {
        if (this.isTracking) {
            this._dirtyValueCalculator.setOriginalValue(this._children);
        }
    }
    getChildProperties(id) {
        return this.properties[id] == null ? {} : this.properties[id];
    }
}
//# sourceMappingURL=parent-to-many-children-relation.js.map