import { AppObjectEntity } from "@vived/core";
import { LabelEntity } from "../../Label/Entities";
export class LabelDropItemEntity extends AppObjectEntity {
    static get(id, appObjects) {
        return appObjects
            .get(id)
            ?.getComponent(LabelDropItemEntity.type);
    }
    get id() {
        return this.appObject.id;
    }
    get data() {
        return {
            id: this.id,
            correctAppObjectIDs: [...this._targetIds],
            label: this.labelEntity.custom,
        };
    }
    set data(val) {
        this.labelEntity.custom = val.label;
        this.targetIds = val.correctAppObjectIDs;
    }
    get label() {
        return this.labelEntity.custom;
    }
    set label(val) {
        this.labelEntity.custom = val;
    }
    get targetValidationMessage() {
        if (this._targetIds.length === 0) {
            return LabelDropItemEntity.LABEL_DROP_NEED_TARGETS;
        }
        else {
            return undefined;
        }
    }
    get targetIds() {
        return [...this._targetIds];
    }
    set targetIds(val) {
        this._targetIds = [...val];
        this.notifyOnChange();
    }
    get tries() {
        return this._tries;
    }
    get answeredCorrectly() {
        return this._answeredCorrectly;
    }
    constructor(appObject) {
        super(appObject, LabelDropItemEntity.type);
        this._targetIds = [];
        this.addTargetID = (id) => {
            if (this._targetIds.includes(id))
                return;
            this._targetIds.push(id);
            this.notifyOnChange();
        };
        this.removeTargetID = (id) => {
            if (!this._targetIds.includes(id))
                return;
            this._targetIds = this._targetIds.filter((existingID) => existingID !== id);
            this.notifyOnChange();
        };
        this._tries = 0;
        this.submitLabelDrop = (onObjectId) => {
            this._tries++;
            this._answeredCorrectly = this._targetIds.includes(onObjectId);
            this.notifyOnChange();
            return this._answeredCorrectly;
        };
        this.reset = () => {
            this._tries = 0;
            this._answeredCorrectly = undefined;
            this.notifyOnChange();
        };
        this.labelEntity = LabelEntity.addIfMissing(appObject);
        this.labelEntity.addChangeObserver(this.notifyOnChange);
    }
}
LabelDropItemEntity.type = "LabelDropItemEntity";
LabelDropItemEntity.LABEL_DROP_EMPTY = "Label Drop answer text cannot be empty";
LabelDropItemEntity.LABEL_DROP_TOO_LONG = "Label Drop answer text is limited to 40 characters";
LabelDropItemEntity.LABEL_DROP_NEED_TARGETS = "Label Drop answer must have at least one correct object";
//# sourceMappingURL=LabelDropItemEntity.js.map