var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import {} from '@blockeditor/store';
import { Bound } from '../utils/bound.js';
import { getBoundsWithRotation } from '../utils/math-utils.js';
import { deserializeXYWH } from '../utils/xywh.js';
import { local, updateDerivedProp, yfield } from './decorators.js';
export class ElementModel {
    static propsToY(props) {
        return props;
    }
    constructor(options) {
        /**
         * When the ymap is not connected to the doc, the value cannot be accessed.
         * But sometimes we need to access the value when creating the element model, those temporary values are stored here.
         */
        this._preserved = new Map();
        this._local = new Map();
        this.display = true;
        this.opacity = 1;
        const { yMap, model, stashedStore, onChange } = options;
        this.yMap = yMap;
        this.surfaceModel = model;
        this._stashed = stashedStore;
        this._onChange = onChange;
        // base class property field is assigned before yMap is set
        // so we need to manually assign the default value here
        this.index = 'a0';
    }
    get deserializedXYWH() {
        return deserializeXYWH(this.xywh);
    }
    get x() {
        return this.deserializedXYWH[0];
    }
    get y() {
        return this.deserializedXYWH[1];
    }
    get w() {
        return this.deserializedXYWH[2];
    }
    get h() {
        return this.deserializedXYWH[3];
    }
    get group() {
        return this.surfaceModel.getGroup(this.id);
    }
    get id() {
        return this.yMap.get('id');
    }
    get elementBound() {
        if (this.rotate) {
            return Bound.from(getBoundsWithRotation(this));
        }
        return Bound.deserialize(this.xywh);
    }
    stash(prop) {
        if (this._stashed.has(prop)) {
            return;
        }
        const curVal = this.yMap.get(prop);
        const prototype = Object.getPrototypeOf(this);
        this._stashed.set(prop, curVal);
        Object.defineProperty(this, prop, {
            configurable: true,
            enumerable: true,
            get: () => this._stashed.get(prop),
            set: (value) => {
                const oldValue = this._stashed.get(prop);
                this._stashed.set(prop, value);
                this._onChange({ [prop]: { oldValue } });
                updateDerivedProp(prototype, prop, this);
            },
        });
    }
    pop(prop) {
        if (!this._stashed.has(prop)) {
            return;
        }
        const value = this._stashed.get(prop);
        this._stashed.delete(prop);
        // @ts-ignore
        delete this[prop];
        this.yMap.set(prop, value);
    }
}
__decorate([
    yfield()
], ElementModel.prototype, "index", void 0);
__decorate([
    local()
], ElementModel.prototype, "display", void 0);
__decorate([
    local()
], ElementModel.prototype, "opacity", void 0);
//# sourceMappingURL=base.js.map