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 { CanvasBase, doc, ignoreTouchEventsProperty, DOMRect } from './common';
import { DOMMatrix } from '../Canvas2D';
import { CanvasRenderingContext2D } from '../Canvas2D/CanvasRenderingContext2D';
import { WebGLRenderingContext } from '../WebGL/WebGLRenderingContext';
import { WebGL2RenderingContext } from '../WebGL2/WebGL2RenderingContext';
import { Application, View, profile, Screen, ImageSource, Utils } from '@nativescript/core';
export function createSVGMatrix() {
    return new DOMMatrix();
}
export * from './common';
const defaultOpts = {
    alpha: true,
    antialias: true,
    depth: true,
    failIfMajorPerformanceCaveat: false,
    powerPreference: 'default',
    premultipliedAlpha: true,
    preserveDrawingBuffer: false,
    stencil: false,
    desynchronized: false,
    xrCompatible: false
};
var ContextType;
(function (ContextType) {
    ContextType[ContextType["None"] = 0] = "None";
    ContextType[ContextType["Canvas"] = 1] = "Canvas";
    ContextType[ContextType["WebGL"] = 2] = "WebGL";
    ContextType[ContextType["WebGL2"] = 3] = "WebGL2";
})(ContextType || (ContextType = {}));
export class Canvas extends CanvasBase {
    constructor(nativeInstance) {
        super();
        this._ready = false;
        this._didPause = false;
        this._contextType = ContextType.None;
        this._is2D = false;
        this._isBatch = false;
        this._drawingBufferHeight = 0;
        this._drawingBufferWidth = 0;
        this._didLayout = false;
        if (nativeInstance) {
            // allows Worker usage
            this._canvas = nativeInstance;
            global.__canvasLoaded = true;
        }
        else {
            const activity = Application.android.foregroundActivity || Application.android.startActivity || Utils.android.getApplicationContext();
            if (Canvas.useSurface) {
                this._canvas = new org.nativescript.canvas.NSCCanvas(activity, org.nativescript.canvas.NSCCanvas.SurfaceType.Surface);
            }
            else {
                this._canvas = new org.nativescript.canvas.NSCCanvas(activity);
            }
            global.__canvasLoaded = true;
            const ref = new WeakRef(this);
            this._canvas.setTouchEventListener(new org.nativescript.canvas.NSCCanvas.TouchEvents({
                onEvent(event, nativeEvent) {
                    const owner = ref.get();
                    if (!owner) {
                        return;
                    }
                    owner._handleEvents(event);
                }
            }));
        }
    }
    [ignoreTouchEventsProperty.setNative](value) {
        this._canvas.setIgnoreTouchEvents(value);
    }
    // @ts-ignore
    get android() {
        return this._canvas;
    }
    get clientWidth() {
        return this.width;
    }
    get clientHeight() {
        return this.height;
    }
    get drawingBufferHeight() {
        return this._drawingBufferHeight;
    }
    get drawingBufferWidth() {
        return this._drawingBufferWidth;
    }
    // @ts-ignore
    get width() {
        return this._logicalSize.width;
    }
    set width(value) {
        if (value === undefined || value === null) {
            this.style.width = 0;
        }
        else {
            this.style.width = value;
        }
        this._didLayout = false;
        this._layoutNative();
    }
    // @ts-ignore
    get height() {
        return this._logicalSize.height;
    }
    set height(value) {
        if (value === undefined || value === null) {
            this.style.height = 0;
        }
        else {
            this.style.height = value;
        }
        this._didLayout = false;
        this._layoutNative();
    }
    static createCustomView() {
        const canvas = new Canvas();
        canvas._isBatch = true;
        canvas._isCustom = true;
        canvas.width = 300;
        canvas.height = 150;
        canvas._isBatch = false;
        canvas._layoutNative();
        return canvas;
    }
    createNativeView() {
        return this._canvas;
    }
    initNativeView() {
        super.initNativeView();
        const ref = new WeakRef(this);
        this.on(View.layoutChangedEvent, (args) => {
            const parent = this.parent;
            // TODO change DIPs once implemented
            if (parent && parent.clientWidth === undefined && parent.clientHeight === undefined) {
                Object.defineProperty(parent, 'clientWidth', {
                    get: function () {
                        return parent.getMeasuredWidth() / Screen.mainScreen.scale;
                    }
                });
                Object.defineProperty(parent, 'clientHeight', {
                    get: function () {
                        return parent.getMeasuredHeight() / Screen.mainScreen.scale;
                    }
                });
            }
            if (parent && typeof parent.getBoundingClientRect !== 'function') {
                parent.getBoundingClientRect = function () {
                    const view = this;
                    const nativeView = view.android;
                    const width = this.width;
                    const height = this.height;
                    const scale = Screen.mainScreen.scale;
                    return {
                        bottom: nativeView.getBottom() / scale,
                        height: height,
                        left: nativeView.getLeft() / scale,
                        right: nativeView.getRight() / scale,
                        top: nativeView.getTop() / scale,
                        width: width,
                        x: nativeView.getX() / scale,
                        y: nativeView.getY() / scale
                    };
                };
            }
            if (parent && parent.ownerDocument === undefined) {
                Object.defineProperty(parent, 'ownerDocument', {
                    get: function () {
                        return global?.window?.document ?? doc;
                    }
                });
            }
        });
        this._canvas.setListener(new org.nativescript.canvas.NSCCanvas.Listener({
            contextReady() {
                const owner = ref.get();
                if (owner && !owner._ready) {
                    owner._ready = true;
                    owner._readyEvent();
                }
            },
            surfaceResize(width, height) {
                // if(this._webglContext || this._webgl2Context){
                // 	(this._webglContext || this._webgl2Context)?.resize();
                // }
                const owner = ref.get();
                if (owner) {
                    owner._drawingBufferWidth = width / Screen.mainScreen.scale;
                    owner._drawingBufferHeight = height / Screen.mainScreen.scale;
                }
            }
        }));
    }
    onUnloaded() {
        this._didPause = true;
        // if (this._canvas) {
        // 	this._canvas.onPause();
        // }
        super.onUnloaded();
    }
    onLoaded() {
        super.onLoaded();
        if (this._didPause) {
            this._didPause = false;
            // if (this._canvas) {
            // 	this._canvas.onResume();
            // }
        }
    }
    disposeNativeView() {
        this._canvas.setListener(null);
        this._canvas = undefined;
        super.disposeNativeView();
    }
    toDataURL(type = 'image/png', encoderOptions = 0.92) {
        return this.native.__toDataURL(type, encoderOptions);
    }
    snapshot(flip = false) {
        if (this._canvas) {
            const bm = this._canvas.snapshot?.(flip ?? false);
            if (bm) {
                return new ImageSource(bm);
            }
        }
        return null;
    }
    _layoutNative() {
        if (!this._isCustom) {
            return;
        }
        if (this._isBatch) {
            return;
        }
        if (this._didLayout) {
            return;
        }
        if (!this.parent) {
            if ((typeof this.width === 'string' && this.width.indexOf('%')) || (typeof this.height === 'string' && this.height.indexOf('%'))) {
                return;
            }
            if (this._canvas === undefined || this._canvas === null) {
                return;
            }
            const size = this._physicalSize;
            org.nativescript.canvas.NSCCanvas.layoutView(size.width || 0, size.height || 0, this._canvas);
            //	this._drawingBufferWidth = size.width || 1;
            //	this._drawingBufferHeight = size.height || 1;
            if (this._is2D) {
                this._2dContext.native.__resize(size.width, size.height);
            }
            this._didLayout = true;
        }
    }
    get __native__context() {
        switch (this._contextType) {
            case ContextType.Canvas:
                return this._2dContext.native;
            case ContextType.WebGL:
                return this._webglContext.native;
            case ContextType.Canvas:
                return this._webgl2Context.native;
            default:
                return null;
        }
    }
    get native() {
        return this.__native__context;
    }
    getContext(type, options) {
        if (!this._canvas) {
            return null;
        }
        if (typeof type === 'string') {
            if (type === '2d') {
                if (this._webglContext || this._webgl2Context) {
                    return null;
                }
                if (!this._2dContext) {
                    this._layoutNative();
                    const opts = {
                        ...defaultOpts, ...this._handleContextOptions(type, options),
                        fontColor: this.parent?.style?.color?.android || -16777216
                    };
                    const ctx = this._canvas.create2DContext(opts.alpha, opts.antialias, opts.depth, opts.failIfMajorPerformanceCaveat, opts.powerPreference, opts.premultipliedAlpha, opts.preserveDrawingBuffer, opts.stencil, opts.desynchronized, opts.xrCompatible, opts.fontColor);
                    this._2dContext = new CanvasRenderingContext2D(ctx);
                    this._2dContext._canvas = this;
                    this._2dContext._type = '2d';
                    this._contextType = ContextType.Canvas;
                    this._is2D = true;
                }
                return this._2dContext;
            }
            else if (type === 'webgl') {
                if (this._2dContext || this._webgl2Context) {
                    return null;
                }
                if (!this._webglContext) {
                    this._layoutNative();
                    const opts = { version: 1, ...defaultOpts, ...this._handleContextOptions(type, options) };
                    this._canvas.initContext(type, opts.alpha, false, opts.depth, opts.failIfMajorPerformanceCaveat, opts.powerPreference, opts.premultipliedAlpha, opts.preserveDrawingBuffer, opts.stencil, opts.desynchronized, opts.xrCompatible);
                    this._webglContext = new WebGLRenderingContext(this._canvas, opts);
                    this._webglContext._canvas = this;
                    this._webglContext._type = 'webgl';
                    this._contextType = ContextType.WebGL;
                }
                return this._webglContext;
            }
            else if (type === 'webgl2' || type === 'experimental-webgl') {
                if (this._2dContext || this._webglContext) {
                    return null;
                }
                if (!this._webgl2Context) {
                    this._layoutNative();
                    const opts = { version: 2, ...defaultOpts, ...this._handleContextOptions(type, options) };
                    this._canvas.initContext(type, opts.alpha, false, opts.depth, opts.failIfMajorPerformanceCaveat, opts.powerPreference, opts.premultipliedAlpha, opts.preserveDrawingBuffer, opts.stencil, opts.desynchronized, opts.xrCompatible);
                    this._webgl2Context = new WebGL2RenderingContext(this._canvas, opts);
                    this._webgl2Context._canvas = this;
                    this._webgl2Context._type = 'webgl2';
                    this._contextType = ContextType.WebGL2;
                }
                return this._webgl2Context;
            }
        }
        return null;
    }
    get _boundingClientRect() {
        if (this._jsBuffer === undefined) {
            this._jsBuffer = new Float32Array(8);
            this._canvas?.setBoundsBuffer?.(this._jsBuffer);
        }
        return this._jsBuffer;
    }
    getBoundingClientRect() {
        if (!this._canvas) {
            return new DOMRect(0, 0, 0, 0);
        }
        org.nativescript.canvas.NSCCanvas.getBoundingClientRect(this._canvas);
        return new DOMRect(this._boundingClientRect[6], this._boundingClientRect[7], this._boundingClientRect[4], this._boundingClientRect[5], this._boundingClientRect[0], this._boundingClientRect[1], this._boundingClientRect[2], this._boundingClientRect[3]);
    }
    setPointerCapture() {
    }
    releasePointerCapture() {
    }
}
Canvas.useSurface = false;
__decorate([
    profile,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], Canvas.prototype, "onLoaded", null);
//# sourceMappingURL=index.android.js.map