import { Property, View } from '@nativescript/core';
import { ImageAsset } from '../ImageAsset';
import { Dom } from './Dom';
import { Paint } from './Paint';
const xProperty = new Property({
    name: 'x',
    valueConverter(value) {
        return parseFloat(value);
    },
    valueChanged(target, oldValue, newValue) {
        target.invalidate();
    },
});
const yProperty = new Property({
    name: 'y',
    valueConverter(value) {
        return parseFloat(value);
    },
    valueChanged(target, oldValue, newValue) {
        target.invalidate();
    },
});
const imageProperty = new Property({
    name: 'image',
    valueChanged(target, oldValue, newValue) {
        // target.invalidate();
        if (typeof newValue === 'string') {
            if (newValue.startsWith('http')) {
                target._loader.fromUrl(newValue);
            }
        }
    },
});
export class Image extends View {
    _addCanvas(canvas) {
        this._canvas = canvas;
    }
    constructor() {
        super();
        this._loader = new ImageAsset();
        this._loader.on('complete', (event) => {
            this.invalidate();
        });
    }
    invalidate() {
        const parent = this.parent;
        if (parent instanceof Dom) {
            parent._dirty?.();
        }
        else if (parent instanceof Paint) {
            parent.invalidate();
        }
    }
    draw() {
        if (this.image === undefined) {
            return;
        }
        const context = this._canvas.getContext('2d');
        context.drawImage(this._loader, this.x, this.y, this.width, this.height);
    }
}
imageProperty.register(Image);
xProperty.register(Image);
yProperty.register(Image);
//# sourceMappingURL=Image.js.map