import { Color, LayoutBase, Property } from '@nativescript/core';
export const paintStyleProperty = new Property({
    name: 'paintStyle',
});
export const strokeWidthProperty = new Property({
    name: 'strokeWidth',
});
export const strokeJoinProperty = new Property({
    name: 'strokeJoin',
});
const defaultColor = new Color('black');
export class Paint extends LayoutBase {
    constructor() {
        super(...arguments);
        this._paintStyleDirty = false;
        this._inGroup = false;
    }
    _addCanvas(canvas) {
        this._canvas = canvas;
    }
    [paintStyleProperty.setNative](value) {
        this._paintStyleDirty = true;
    }
    invalidate() {
        const parent = this.parent;
        if (parent != null) {
            parent._dirty?.();
        }
    }
    _getStrokeJoin() {
        return this.parent?.strokeJoin ?? this.strokeJoin ?? 'miter';
    }
    _getStrokeWidth() {
        return this.parent?.strokeWidth ?? this.strokeWidth ?? 1;
    }
    _getPaintStyle() {
        const paintStyle = this.paintStyle;
        if (paintStyle === 'stroke') {
            return paintStyle;
        }
        if (this._inGroup) {
            return this.parent?._getPaintStyle?.();
        }
        return 'fill';
    }
    _getColor() {
        const color = this.color ?? defaultColor;
        const hex = color.hex;
        if (color.name !== 'black' && hex !== '#000000') {
            return hex;
        }
        return this.parent?.color?.hex ?? hex;
    }
    draw() {
        const color = this._getColor();
        const context = this._canvas.getContext('2d');
        const style = this._getPaintStyle();
        //context.closePath();
        context.globalAlpha = this.opacity;
        if (style === 'fill') {
            context.fillStyle = color;
            context.fill();
        }
        else if (style === 'stroke') {
            context.lineWidth = this._getStrokeWidth();
            context.strokeStyle = color;
            context.stroke();
        }
        //context.beginPath();
    }
}
paintStyleProperty.register(Paint);
strokeWidthProperty.register(Paint);
strokeJoinProperty.register(Paint);
//# sourceMappingURL=Paint.js.map