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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Http, View, Style, CssProperty, Property, path, knownFolders, CSSType, Application, Utils } from '@nativescript/core';
export const strokeProperty = new CssProperty({
    name: 'stroke',
    cssName: 'stroke',
    defaultValue: undefined,
});
export const strokeWidthProperty = new CssProperty({
    name: 'strokeWidth',
    cssName: 'stroke-width',
    defaultValue: 1,
});
export const fillProperty = new CssProperty({
    name: 'fill',
    cssName: 'fill',
    defaultValue: undefined,
});
export const fillRuleProperty = new CssProperty({
    name: 'fillRule',
    cssName: 'fill-rule',
    defaultValue: undefined,
});
export const fillOpacityProperty = new CssProperty({
    name: 'fillOpacity',
    cssName: 'fill-opacity',
    defaultValue: undefined,
});
export const stopColorProperty = new CssProperty({
    name: 'stopColor',
    cssName: 'stop-color',
    defaultValue: undefined,
});
export const strokeLinecapProperty = new CssProperty({
    name: 'strokeLinecap',
    cssName: 'stroke-linecap',
});
export const strokeLinejoinProperty = new CssProperty({
    name: 'strokeLinejoin',
    cssName: 'stroke-linejoin',
});
export const strokeMiterlimitProperty = new CssProperty({
    name: 'strokeMiterlimit',
    cssName: 'stroke-miterlimit',
});
export const srcProperty = new Property({
    name: 'src',
});
import { DOMParser, XMLSerializer } from 'xmldom';
const initialSVG = '<svg width="auto" height="auto" xmlns="http://www.w3.org/2000/svg"></svg>';
let Svg = class Svg extends View {
    constructor() {
        super();
        this.__children = [];
        this.__attachedToDom = false;
        if (global.isAndroid) {
            const activity = Application.android.foregroundActivity || Application.android.startActivity;
            this._svg = new org.nativescript.canvas.NSCSVG(activity);
        }
        else if (global.isIOS) {
            this._svg = TNSSVG.alloc().initWithFrame(CGRectZero);
            this._svg.backgroundColor = UIColor.clearColor;
        }
        this._dom = new DOMParser().parseFromString(initialSVG);
        this._serializer = new XMLSerializer();
    }
    [srcProperty.setNative](value) {
        if (typeof value === 'string') {
            if (value.indexOf('<svg') > -1) {
                if (global.isAndroid) {
                    this._svg.setSrc(value);
                }
                else if (global.isIOS) {
                    this._svg.src = value;
                }
            }
            else {
                if (value.startsWith('~')) {
                    if (global.isAndroid) {
                        this._svg.setSrcPath(path.join(knownFolders.currentApp().path, value.replace('~', '')));
                    }
                    else if (global.isIOS) {
                        this._svg.srcPath = path.join(knownFolders.currentApp().path, value.replace('~', ''));
                    }
                }
                else if (value.startsWith('/')) {
                    if (global.isAndroid) {
                        this._svg.setSrcPath(value);
                    }
                    else if (global.isIOS) {
                        this._svg.srcPath = value;
                    }
                }
                else if (value.startsWith('http')) {
                    Http.getString(value)
                        .then((res) => {
                        if (global.isAndroid) {
                            this._svg.setSrc(res);
                        }
                        else if (global.isIOS) {
                            this._svg.src = res;
                        }
                    })
                        .catch((e) => {
                        console.log(e);
                    });
                }
            }
        }
    }
    onLayout(left, top, right, bottom) {
        super.onLayout(left, top, right, bottom);
        this.__redraw();
    }
    onMeasure(widthMeasureSpec, heightMeasureSpec) {
        const nativeView = this.nativeView;
        if (nativeView) {
            const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
            const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
            this.setMeasuredDimension(width, height);
        }
    }
    __redraw() {
        if (this.__attachedToDom) {
            const domCopy = this._dom.valueOf();
            const serialized = this._serializer.serializeToString(domCopy);
            const width = domCopy.documentElement.getAttribute('width');
            const height = domCopy.documentElement.getAttribute('height');
            if (width === 'auto') {
                domCopy.documentElement.setAttribute('width', `${this.getMeasuredWidth()}px`);
            }
            if (height === 'auto') {
                domCopy.documentElement.setAttribute('height', `${this.getMeasuredHeight()}px`);
            }
            if (serialized !== initialSVG) {
                this.src = serialized;
            }
        }
    }
    onLoaded() {
        super.onLoaded();
        this.__attachedToDom = true;
    }
    onUnloaded() {
        this.__attachedToDom = false;
        super.onUnloaded();
    }
    createNativeView() {
        return this._svg;
    }
    initNativeView() {
        super.initNativeView();
    }
    addChild(view) {
        this._addView(view);
        view.__attached = true;
        this.__children.push(view);
    }
    removeChild(view) {
        if (view.__attached) {
            this._removeView(view);
            view.__attached = false;
            this.__children = this.__children.filter((item) => item !== view);
        }
    }
};
Svg.readyEvent = 'ready';
Svg = __decorate([
    CSSType('Svg'),
    __metadata("design:paramtypes", [])
], Svg);
export { Svg };
srcProperty.register(Svg);
stopColorProperty.register(Style);
strokeWidthProperty.register(Style);
strokeProperty.register(Style);
fillProperty.register(Style);
fillOpacityProperty.register(Style);
fillRuleProperty.register(Style);
strokeLinecapProperty.register(Style);
strokeLinejoinProperty.register(Style);
strokeMiterlimitProperty.register(Style);
//# sourceMappingURL=SVG.js.map