import { ImageAsset } from '../ImageAsset';
import { native_ } from './Constants';
export class GPUQueue {
    get label() {
        return this[native_]?.label ?? '';
    }
    static fromNative(value) {
        if (value) {
            const ret = new GPUQueue();
            ret[native_] = value;
            return ret;
        }
        return null;
    }
    copyExternalImageToTexture(source, destination, copySize) {
        const src = {
            source: undefined,
        };
        if (source.source) {
            if (source.source instanceof ImageBitmap) {
                src.source = source.source.native;
            }
            else if (source.source instanceof ImageData) {
                src.source = source.source.native;
            }
            else if (source.source instanceof ImageAsset) {
                src.source = source.source.native;
            }
            else if (typeof source.source.tagName === 'string' && (source.source.tagName === 'IMG' || source.source.tagName === 'IMAGE')) {
                if (source.source._asset instanceof ImageAsset) {
                    src.source = source.source._asset.native;
                }
            }
        }
        if (typeof source.flipY === 'boolean') {
            src.flipY = source.flipY;
        }
        if (source.origin) {
            src.origin = { ...source.origin };
        }
        const dst = {
            texture: destination.texture[native_],
        };
        if (destination.aspect) {
            dst.aspect = destination.aspect;
        }
        if (destination.colorSpace) {
            dst.colorSpace = destination.colorSpace;
        }
        if (typeof destination.mipLevel === 'number') {
            dst.mipLevel = destination.mipLevel;
        }
        if (destination.origin) {
            dst.origin = { ...destination.origin };
        }
        if (typeof destination.premultipliedAlpha === 'boolean') {
            dst.premultipliedAlpha = destination.premultipliedAlpha;
        }
        let size;
        if (Array.isArray(copySize)) {
            size = {
                width: copySize[0],
                height: copySize[1] ?? 1,
                depthOrArrayLayers: copySize[2] ?? 1,
            };
        }
        else {
            size = {
                width: 0,
                height: 1,
                depthOrArrayLayers: 1,
                ...copySize,
            };
        }
        this[native_].copyExternalImageToTexture(src, dst, size);
    }
    submit(commands) {
        if (Array.isArray(commands)) {
            this[native_].submit(commands.map((command) => {
                return command[native_];
            }));
        }
    }
    onSubmittedWorkDone() {
        return new Promise((resolve, reject) => {
            this[native_].onSubmittedWorkDone(() => {
                resolve();
            });
        });
    }
    writeBuffer(buffer, bufferOffset, data, dataOffset, size) {
        if (Array.isArray(data)) {
            data = new Uint8Array(data);
        }
        else if (ArrayBuffer.isView(data)) {
            data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
        }
        else if (data instanceof ArrayBuffer) {
            data = new Uint8Array(data);
        }
        this[native_].writeBuffer(buffer?.[native_], bufferOffset ?? 0, data, dataOffset ?? 0, size ?? -1);
    }
    writeTexture(destination, data, dataLayout, size) {
        destination.texture = destination.texture[native_];
        if (Array.isArray(size)) {
            size = {
                width: size[0],
                height: size[1] ?? 1,
                depthOrArrayLayers: size[2] ?? 1,
            };
        }
        this[native_].writeTexture(destination, data, dataLayout, size);
    }
}
//# sourceMappingURL=GPUQueue.js.map