import { Property, ShorthandProperty, Style } from '@nativescript/core';
import { Paint } from '../Paint';
export const cxProperty = new Property({
    name: 'cx',
    valueConverter(value) {
        return parseFloat(value);
    },
    valueChanged(target, oldValue, newValue) {
        target.invalidate();
    },
});
export const cyProperty = new Property({
    name: 'cy',
    valueConverter(value) {
        return parseFloat(value);
    },
    valueChanged(target, oldValue, newValue) {
        target.invalidate();
    },
});
export const rProperty = new Property({
    name: 'r',
    valueConverter(value) {
        return parseFloat(value);
    },
    valueChanged(target, oldValue, newValue) {
        target.invalidate();
    },
});
export const cProperty = new ShorthandProperty({
    name: 'c',
    cssName: 'c',
    getter: function () {
        return { x: this.cx, y: this.cy };
    },
    converter(value) {
        if (typeof value === 'string') {
            try {
                const val = JSON.parse(value);
                return [
                    [cxProperty, val.x],
                    [cyProperty, val.y],
                ];
            }
            catch (error) { }
        }
        else {
            return [
                [cxProperty, value.x],
                [cyProperty, value.y],
            ];
        }
        return [];
    },
});
export class Circle extends Paint {
    constructor() {
        super(...arguments);
        this._children = [];
    }
    get c() {
        return this.style.c;
    }
    set c(value) {
        this.style.c = value;
    }
    draw() {
        const context = this._canvas.getContext('2d');
        const c = this.c;
        context.arc(c.x ?? this.cx, c.y ?? this.cy, this.r, 0, 2 * Math.PI);
        if (this._children.length > 0) {
            this._children.forEach((child) => {
                const color = child._getColor();
                const style = child._getPaintStyle();
                switch (style) {
                    case 'fill':
                        context.fillStyle = color;
                        context.fill();
                        break;
                    case 'stroke':
                        context.strokeStyle = color;
                        context.lineWidth = child._getStrokeWidth();
                        context.lineJoin = child._getStrokeJoin();
                        context.stroke();
                        break;
                }
            });
        }
        else {
            super.draw();
        }
    }
    _addViewToNativeVisualTree(view, atIndex) {
        if (view === this._canvas) {
            this.nativeView.addView(this._canvas.nativeView);
            return true;
        }
        else if (view instanceof Paint) {
            view._canvas = this._canvas;
            this._children.push(view);
        }
        return false;
    }
}
cxProperty.register(Circle);
cyProperty.register(Circle);
rProperty.register(Circle);
cProperty.register(Style);
//# sourceMappingURL=Circle.js.map