import { SVGBase, srcProperty, syncProperty, initialSVG } from './common';
import { Http, knownFolders, path, Utils } from '@nativescript/core';
export * from './Elements';
export class Svg extends SVGBase {
    constructor() {
        super();
        this._svg = TNSSVG.alloc().initWithFrame(CGRectZero);
        this._svg.backgroundColor = UIColor.clearColor;
    }
    createNativeView() {
        return this._svg;
    }
    get native() {
        return this._svg;
    }
    [srcProperty.setNative](value) {
        if (typeof value === 'string') {
            if (value.indexOf('<svg') > -1) {
                this._svg.src = value;
            }
            else {
                if (value.startsWith('~')) {
                    this._svg.srcPath = path.join(knownFolders.currentApp().path, value.replace('~', ''));
                }
                else if (value.startsWith('/')) {
                    this._svg.srcPath = value;
                }
                else if (value.startsWith('http')) {
                    Http.getFile(value)
                        .then((res) => {
                        this._svg.srcPath = res.path;
                    })
                        .catch((e) => {
                        console.log(e);
                    });
                }
            }
        }
    }
    [syncProperty.setNative](value) {
        this._svg.sync = value;
    }
    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();
    }
    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);
        }
    }
}
//# sourceMappingURL=index.ios.js.map