import { MergableObjectEventSource, MergableObjectState, MergableObjectConflict } from '../../MergableSet';
import { MarkupEventTypes } from '../MarkupEntityTypes';
import { MarkupEntityBase } from './MarkupEntityBase';
import { MarkupSheet2D } from './MarkupSheet2D';
/**
 * Super class for markup entities in the markup API.
 */
export class MarkupEntity extends MarkupEntityBase {
    _sheet;
    _enabled = true;
    /**
     * Do not use. Use {@link MarkupSheet2D.add} instead.
     * @hidden
     */
    constructor(id, _sheet, properties = new Map()) {
        super(id, properties);
        this._sheet = _sheet;
    }
    /** Returns the markup sheet that the entity is attached to */
    get sheet() {
        return this._sheet;
    }
    /** @inheritDoc */
    get isEnabled() {
        return this._sheet.isEnabled && this._enabled;
    }
    /** @inheritDoc */
    set isEnabled(b) {
        this._enabled = b;
    }
    /** @inheritDoc */
    onAdded(o) {
        this.detectParentSheetConflicts(o);
        this._sheet.parent.observable.notifyObservers({ eventType: MarkupEventTypes.Added, ...o });
    }
    /** @inheritDoc */
    onUpdate(o) {
        this.detectParentSheetConflicts(o);
        this._sheet.parent.observable.notifyObservers({ eventType: MarkupEventTypes.Update, ...o });
    }
    /** @inheritDoc */
    onDelete(o) {
        this._sheet.parent.observable.notifyObservers({ eventType: MarkupEventTypes.Delete, ...o });
    }
    detectParentSheetConflicts(o) {
        if (o.eventSource === MergableObjectEventSource.Remote && this.assignParentSheet()) {
            const sheetMergableObject = this._sheet.parent.get(this._sheet.id);
            if (!sheetMergableObject) {
                throw new Error('Sheet does not exist in layer, this should be impossible (probably a bug).');
            }
            if (sheetMergableObject.state === MergableObjectState.Deleted && !sheetMergableObject.hasConflict) {
                sheetMergableObject.conflictReason = MergableObjectConflict.RemoteDiffers;
                sheetMergableObject.local.onUpdate({
                    conflictReason: MergableObjectConflict.RemoteDiffers,
                    remote: sheetMergableObject.remote,
                    local: sheetMergableObject.local,
                    eventSource: MergableObjectEventSource.Remote,
                    state: sheetMergableObject.state
                });
            }
        }
    }
    assignParentSheet() {
        const tmpSheet = this._sheet.parent._layer.get(this._sheet.id);
        if (!tmpSheet) {
            throw new Error('Layer does not exist.');
        }
        if (tmpSheet.local instanceof MarkupSheet2D) {
            this._sheet = tmpSheet.local;
            return true;
        }
        throw new Error('Markup sheet was not of type MarkupSheet2D.');
    }
    /** @inheritDoc */
    markAsDeleted() {
        return this._sheet.parent._layer.delete(this.id);
    }
    /** @inheritDoc */
    markAsUpdated() {
        this._sheet.markAsUpdated();
        return this._sheet.parent._layer.update(this.id, () => { });
    }
}
//# sourceMappingURL=MarkupEntity.js.map