import { WebGLQuery } from '../WebGLQuery';
import { WebGLSampler } from '../WebGLSampler';
import { WebGLSync } from '../WebGLSync';
import { WebGLTransformFeedback } from '../WebGLTransformFeedback';
import { WebGLVertexArrayObject } from '../WebGLVertexArrayObject';
import { WebGLFramebuffer } from '../../WebGL/WebGLFramebuffer';
import { WebGLActiveInfo } from '../../WebGL/WebGLActiveInfo';
import { WebGLBuffer } from '../../WebGL/WebGLBuffer';
import { ImageAsset } from '../../ImageAsset';
import { ImageSource, Screen } from '@nativescript/core';
import { WebGL2RenderingContextBase } from './common';
import { Canvas } from '../../Canvas';
import { ImageBitmap } from '../../ImageBitmap';
import { Helpers } from '../../helpers';
export class WebGL2RenderingContext extends WebGL2RenderingContextBase {
    constructor(context, contextOptions) {
        super(null);
        let nativeContext = 0;
        if (global.isAndroid) {
            nativeContext = context.getNativeContext().toString();
        }
        if (global.isIOS) {
            nativeContext = context.nativeContext.toString();
        }
        const ctx = BigInt(nativeContext);
        let direction = 0;
        if (global.isAndroid) {
            direction = org.nativescript.canvas.NSCCanvas.getDirection();
        }
        else {
            //direction = 1;
        }
        const native = global.CanvasModule.createWebGL2Context(contextOptions, ctx, Screen.mainScreen.scale, -16777216, Screen.mainScreen.scale * 160, direction);
        this._context = native;
    }
    /* Transform feedback */
    beginQuery(target, query) {
        const value = query.native;
        this.native.beginQuery(target, value);
    }
    beginTransformFeedback(primitiveMode) {
        this.native.beginTransformFeedback(primitiveMode);
    }
    bindBufferBase(target, index, buffer) {
        const value = buffer ? buffer.native : null;
        this.native.bindBufferBase(target, index, value);
    }
    bindBufferRange(target, index, buffer, offset, size) {
        const value = buffer ? buffer.native : null;
        this.native.bindBufferRange(target, index, value, offset, size);
    }
    bindSampler(unit, sampler) {
        const value = sampler ? sampler.native : null;
        this.native.bindSampler(unit, value);
    }
    bindTransformFeedback(target, transformFeedback) {
        const value = transformFeedback ? transformFeedback.native : null;
        this.native.bindTransformFeedback(target, value);
    }
    bindVertexArray(vertexArray) {
        const value = vertexArray ? vertexArray.native : null;
        this.native.bindVertexArray(value);
    }
    blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter) {
        this.native.blitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
    }
    clearBufferfi(buffer, drawbuffer, depth, stencil) {
        const value = buffer.native;
        this.native.clearBufferfi(value, drawbuffer, depth, stencil);
    }
    clearBufferfv(buffer, drawbuffer, values) {
        const value = buffer.native;
        this.native.clearBufferfv(value, drawbuffer, values);
    }
    clearBufferiv(buffer, drawbuffer, values) {
        const value = buffer ? buffer.native : 0;
        this.native.clearBufferiv(value, drawbuffer, values);
    }
    clearBufferuiv(buffer, drawbuffer, values) {
        const value = buffer ? buffer.native : 0;
        this.native.clearBufferuiv(value, drawbuffer, values);
    }
    clientWaitSync(sync, flags, timeout) {
        const value = sync.native;
        return this.native.clientWaitSync(value, flags, timeout);
    }
    compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, srcData, srcOffset = 0, srcLengthOverride = 0) {
        this.native.compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, srcData, srcOffset);
    }
    /* Transform feedback */
    /* Framebuffers and renderbuffers */
    copyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size) {
        this.native.copyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size);
    }
    copyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height) {
        this.native.copyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height);
    }
    createQuery() {
        return new WebGLQuery(this.native.createQuery());
    }
    createSampler() {
        return new WebGLSampler(this.native.createSampler());
    }
    createTransformFeedback() {
        return new WebGLTransformFeedback(this.native.createTransformFeedback());
    }
    createVertexArray() {
        return new WebGLVertexArrayObject(this.native.createVertexArray());
    }
    deleteQuery(query) {
        const value = query.native;
        this.native.deleteQuery(value);
    }
    deleteSampler(sampler) {
        const value = sampler.native;
        this.native.deleteSampler(value);
    }
    deleteSync(sync) {
        const value = sync.native;
        this.native.deleteSync(value);
    }
    deleteTransformFeedback(transformFeedback) {
        const value = transformFeedback.native;
        this.native.deleteTransformFeedback(value);
    }
    deleteVertexArray(vertexArray) {
        const value = vertexArray.native;
        this.native.deleteVertexArray(value);
    }
    drawArraysInstanced(mode, first, count, instanceCount) {
        this.native.drawArraysInstanced(mode, first, count, instanceCount);
    }
    drawBuffers(buffers) {
        this._checkArgs('drawBuffers', arguments);
        this.native.drawBuffers(buffers);
    }
    drawElementsInstanced(mode, count, type, offset, instanceCount) {
        this.native.drawElementsInstanced(mode, count, type, offset, instanceCount);
    }
    drawRangeElements(mode, start, end, count, type, offset) {
        this.native.drawRangeElements(mode, start, end, count, type, offset);
    }
    endQuery(target) {
        this.native.endQuery(target);
    }
    endTransformFeedback() {
        this.native.endTransformFeedback();
    }
    fenceSync(condition, flags) {
        return new WebGLSync(this.native.fenceSync(condition, flags));
    }
    framebufferTextureLayer(target, attachment, texture, level, layer) {
        const value = texture.native;
        this.native.framebufferTextureLayer(target, attachment, value, level, layer);
    }
    /* Framebuffers and renderbuffers */
    /* Uniforms */
    getActiveUniformBlockName(program, uniformBlockIndex) {
        const value = program.native;
        return this.native.getActiveUniformBlockName(value, uniformBlockIndex);
    }
    getActiveUniformBlockParameter(program, uniformBlockIndex, pname) {
        const value = program.native;
        return this.native.getActiveUniformBlockParameter(value, uniformBlockIndex, pname);
    }
    getActiveUniforms(program, uniformIndices, pname) {
        const value = program.native;
        return this.native.getActiveUniforms(value, uniformIndices, pname);
    }
    getBufferSubData(target, srcByteOffset, dstData, dstOffset = 0, length = 0) {
        this.native.getBufferSubData(target, srcByteOffset, dstData, dstOffset, length);
    }
    getFragDataLocation(program, name) {
        const value = program.native;
        const result = this.native.getFragDataLocation(value, name);
        return result !== -1 ? result : null;
    }
    getIndexedParameter(target, index) {
        const param = this.native.getIndexedParameter(target, index);
        if (target === this.TRANSFORM_FEEDBACK_BUFFER_BINDING || target === this.UNIFORM_BUFFER_BINDING) {
            return new WebGLBuffer(param);
        }
        return param;
    }
    getInternalformatParameter(target, internalformat, pname) {
        return this.native.getInternalformatParameter(target, internalformat, pname);
    }
    getQueryParameter(query, pname) {
        const value = query.native;
        const result = this.native.getQueryParameter(value, pname);
        if (result === 0) {
            return null;
        }
        return result;
    }
    //@ts-ignore
    getParameter(pname) {
        this._checkArgs('getParameter', arguments);
        const value = this.native.getParameter(pname);
        switch (pname) {
            case this.COPY_READ_BUFFER_BINDING:
            case this.COPY_WRITE_BUFFER_BINDING:
                if (value) {
                    return new WebGLBuffer(value);
                }
                return null;
            case this.DRAW_FRAMEBUFFER_BINDING:
                if (value) {
                    return new WebGLFramebuffer(value);
                }
                return null;
            default:
                return this._handleGetParameter(pname, value);
        }
    }
    getQuery(target, pname) {
        const query = this.native.getQuery(target, pname);
        if (query) {
            return new WebGLQuery(query);
        }
        return null;
    }
    getSamplerParameter(sampler, pname) {
        const value = sampler.native;
        return this.native.getSamplerParameter(value, pname);
    }
    getSyncParameter(sync, pname) {
        const value = sync.native;
        return this.native.getSyncParameter(value, pname);
    }
    getTransformFeedbackVarying(program, index) {
        const value = program.native;
        const info = this.native.getTransformFeedbackVarying(value, index);
        if (info) {
            return new WebGLActiveInfo(info);
        }
        return null;
    }
    getUniformBlockIndex(program, uniformBlockName) {
        const value = program.native;
        return this.native.getUniformBlockIndex(value, uniformBlockName);
    }
    getUniformIndices(program, uniformNames) {
        const value = program.native;
        return this.native.getUniformIndices(value, uniformNames);
    }
    invalidateFramebuffer(target, attachments) {
        this.native.invalidateFramebuffer(target, attachments);
    }
    invalidateSubFramebuffer(target, attachments, x, y, width, height) {
        this.native.invalidateSubFramebuffer(target, attachments, x, y, width, height);
    }
    isQuery(query) {
        const value = query.native;
        return this.native.isQuery(value);
    }
    isSampler(sampler) {
        const value = sampler.native;
        return this.native.isSampler(value);
    }
    isSync(sync) {
        const value = sync.native;
        return this.native.isSync(value);
    }
    isTransformFeedback(transformFeedback) {
        const value = transformFeedback.native;
        return this.native.isTransformFeedback(value);
    }
    isVertexArray(vertexArray) {
        const value = vertexArray.native;
        return this.native.isVertexArray(value);
    }
    pauseTransformFeedback() {
        this.native.pauseTransformFeedback();
    }
    readBuffer(src) {
        this.native.readBuffer(src);
    }
    renderbufferStorageMultisample(target, samples, internalFormat, width, height) {
        this.native.renderbufferStorageMultisample(target, samples, internalFormat, width, height);
    }
    resumeTransformFeedback() {
        this.native.resumeTransformFeedback();
    }
    samplerParameterf(sampler, pname, param) {
        const value = sampler.native;
        this.native.samplerParameterf(value, pname, param);
    }
    /* Uniforms */
    /* Sync objects */
    samplerParameteri(sampler, pname, param) {
        const value = sampler.native;
        this.native.samplerParameteri(value, pname, param);
    }
    texImage3D(target, level, internalformat, width, height, depth, border, format, type, source) {
        if (typeof source === 'number') {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source);
        }
        else if (source && source.buffer) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source);
        }
        else if (source instanceof android.graphics.Bitmap) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source);
        }
        else if (source instanceof ImageSource) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source.android);
        }
        else if (source instanceof ImageAsset) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source.native);
        }
        else if (source instanceof Canvas) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source.native);
        }
        else if (source instanceof ImageBitmap) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source.native);
        }
        else if (source && typeof source.tagName === 'string' && (source.tagName === 'IMG' || source.tagName === 'IMAGE')) {
            if (source._imageSource instanceof ImageSource) {
                this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source._imageSource.android);
            }
            else if (source._image instanceof android.graphics.Bitmap) {
                this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source._image);
            }
            else if (source._asset instanceof ImageAsset) {
                this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source._asset.native);
            }
            else if (typeof source.src === 'string') {
                const result = ImageSource.fromFileSync(source.src);
                this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, result ? result.android : null);
            }
        }
        else if (source && typeof source.tagName === 'string' && source.tagName === 'CANVAS' && source._canvas instanceof Canvas) {
            this.native.texImage3D(target, level, internalformat, width, height, depth, border, format, type, source._canvas.native);
        }
    }
    texStorage2D(target, levels, internalformat, width, height) {
        this.native.texStorage2D(target, levels, internalformat, width, height);
    }
    texStorage3D(target, levels, internalformat, width, height, depth) {
        this.native.texStorage3D(target, levels, internalformat, width, height, depth);
    }
    texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData, srcOffset = 0) {
        if (typeof srcData === 'number') {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData);
        }
        else if (srcData && srcData.buffer) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData, srcOffset);
        }
        else if (srcData instanceof android.graphics.Bitmap) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData);
        }
        else if (srcData instanceof ImageSource) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData.android);
        }
        else if (srcData instanceof ImageAsset) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData.native);
        }
        else if (srcData instanceof Canvas) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData.native);
        }
        else if (srcData instanceof ImageBitmap) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData.native);
        }
        else if (srcData && typeof srcData.tagName === 'string' && (srcData.tagName === 'IMG' || srcData.tagName === 'IMAGE')) {
            if (srcData._imageSource instanceof ImageSource) {
                this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData._imageSource.android);
            }
            else if (srcData._image instanceof android.graphics.Bitmap) {
                this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData._image);
            }
            else if (srcData._asset instanceof ImageAsset) {
                this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData._asset.native);
            }
            else if (typeof srcData.src === 'string') {
                const result = ImageSource.fromFileSync(srcData.src);
                this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, result ? result.android : null);
            }
        }
        else if (srcData && typeof srcData.tagName === 'string' && srcData.tagName === 'CANVAS' && srcData._canvas instanceof Canvas) {
            this.native.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, srcData._canvas.native);
        }
    }
    transformFeedbackVaryings(program, varyings, bufferMode) {
        const value = program.native;
        this.native.transformFeedbackVaryings(value, varyings, bufferMode);
    }
    uniform1ui(location, v0) {
        const value = location.native;
        this.native.uniform1ui(value, v0);
    }
    uniform1uiv(location, data) {
        const value = location.native;
        this.native.uniform1uiv(value, data);
    }
    uniform2ui(location, v0, v1) {
        const value = location.native;
        this.native.uniform2ui(value, v0, v1);
    }
    uniform2uiv(location, data) {
        const value = location.native;
        this.native.uniform2uiv(value, data);
    }
    /* Sync objects */
    /* Miscellaneous constants */
    uniform3ui(location, v0, v1, v2) {
        const value = location.native;
        this.native.uniform3ui(value, v0, v1, v2);
    }
    uniform3uiv(location, data) {
        const value = location.native;
        this.native.uniform3uiv(value, data);
    }
    uniform4ui(location, v0, v1, v2, v3) {
        const value = location.native;
        this.native.uniform4ui(value, v0, v1, v2, v3);
    }
    uniform4uiv(location, data) {
        const value = location.native;
        this.native.uniform4uiv(value, data);
    }
    uniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding) {
        const value = program.native;
        this.native.uniformBlockBinding(value, uniformBlockIndex, uniformBlockBinding);
    }
    uniformMatrix2x3fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix2x3fv(value, transpose, data);
    }
    uniformMatrix2x4fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix2x4fv(value, transpose, data);
    }
    uniformMatrix3x2fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix3x2fv(value, transpose, data);
    }
    uniformMatrix3x4fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix3x4fv(value, transpose, data);
    }
    uniformMatrix4x2fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix4x2fv(value, transpose, data);
    }
    uniformMatrix4x3fv(location, transpose, data) {
        const value = location.native;
        this.native.uniformMatrix4x3fv(value, transpose, data);
    }
    vertexAttribDivisor(index, divisor) {
        this.native.vertexAttribDivisor(index, divisor);
    }
    vertexAttribI4i(index, v0, v1, v2, v3) {
        this.native.vertexAttribI4i(index, v0, v1, v2, v3);
    }
    vertexAttribI4iv(index, value) {
        this.native.vertexAttribI4iv(index, value);
    }
    vertexAttribI4ui(index, v0, v1, v2, v3) {
        this.native.vertexAttribI4ui(index, v0, v1, v2, v3);
    }
    vertexAttribI4uiv(index, value) {
        this.native.vertexAttribI4uiv(index, value);
    }
}
(() => {
    Helpers.initialize();
})();
//# sourceMappingURL=index.js.map