import { ClampToEdgeWrapping, DataTexture, FloatType, LinearFilter, NearestFilter } from "three";
import { assert } from "../../../../core/assert.js";
import { BinaryDataType } from "../../../../core/binary/type/BinaryDataType.js";
import { channelCountToThreeTextureFormat } from "../channelCountToThreeTextureFormat.js";
import { computeThreeTextureInternalFormatFromDataType } from "../computeThreeTextureInternalFormatFromDataType.js";

/**
 *
 * @param {Sampler2D} sampler
 * @returns {DataTexture}
 */
export function sampler2d_to_float32_texture(sampler) {
    assert.ok(sampler.data instanceof Float32Array);

    const width = sampler.width;
    const height = sampler.height;

    const texture = new DataTexture(sampler.data, width, height);

    texture.wrapS = ClampToEdgeWrapping;
    texture.wrapT = ClampToEdgeWrapping;

    texture.generateMipmaps = false;

    texture.minFilter = LinearFilter;
    texture.magFilter = NearestFilter;

    texture.flipY = false;

    texture.type = FloatType;
    texture.format = channelCountToThreeTextureFormat(sampler.itemSize);
    texture.internalFormat = computeThreeTextureInternalFormatFromDataType(BinaryDataType.Float32, sampler.itemSize);

    texture.needsUpdate = true;

    return texture;
}
