/* eslint-disable @typescript-eslint/no-explicit-any */
const propertiesSymbol = Symbol('properties');
/**
 * Reactive decorator that calls the `render()` function when the value of the property change.
 */
export default function reactive() {
    return (target, context) => {
        const { kind, name } = context;
        if (kind === 'accessor') {
            // const accessorTarget = target as ClassAccessorDecoratorTarget<C, V>
            return {
                set(value) {
                    let map = Reflect.get(this, propertiesSymbol);
                    if (!map) {
                        map = {};
                        Reflect.set(this, propertiesSymbol, map);
                    }
                    if (map[name] === value) {
                        return;
                    }
                    if (value === undefined) {
                        // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
                        delete map[name];
                    }
                    else {
                        map[name] = value;
                    }
                    if (typeof this.render === 'function') {
                        this.render();
                    }
                },
                get() {
                    const map = Reflect.get(this, propertiesSymbol) || {};
                    return map[context.name];
                },
                init(initialValue) {
                    let map = Reflect.get(this, propertiesSymbol);
                    if (!map) {
                        map = {};
                        Reflect.set(this, propertiesSymbol, map);
                    }
                    map[name] = initialValue;
                    return initialValue;
                },
            };
        }
        else if (kind === 'setter') {
            const { name } = context;
            return function (value) {
                const oldValue = this[name];
                if (value === oldValue) {
                    return;
                }
                ;
                target.call(this, value);
                if (typeof this.render === 'function') {
                    this.render();
                }
            };
        }
        throw new Error(`Unsupported decorator location: ${kind}`);
    };
}
//# sourceMappingURL=reactive.js.map