import {
    LinearEncoding,
    LinearFilter,
    LinearMipmapLinearFilter,
    NearestFilter,
    NearestMipMapLinearFilter,
    sRGBEncoding,
    UnsignedByteType
} from "three";
import { vec3_uint8_to_float } from "../vec3_uint8_to_float.js";
import { apply_texture_clamping_to_coordinate } from "./apply_texture_clamping_to_coordinate.js";

/**
 *
 * @param {number[]|Float32Array} out
 * @param {Sampler2D} sampler
 * @param {Texture} texture
 * @param {number} u
 * @param {number} v
 */
export function sample_texture(out, sampler, texture, u, v) {
    // TODO apply texture matrix transform

    const _u = apply_texture_clamping_to_coordinate(texture.wrapS, u);
    const _v = apply_texture_clamping_to_coordinate(texture.wrapT, v);

    const magFilter = texture.magFilter;

    switch (magFilter) {
        default:
        case NearestFilter:
        case NearestMipMapLinearFilter:
            sampler.sampleNearestUV(_u, _v, out);
            break;

        case LinearFilter:
        case LinearMipmapLinearFilter:

            sampler.sampleBilinearUV(_u, _v, out);
            break;
    }

    if (texture.type === UnsignedByteType) {
        vec3_uint8_to_float(out, out);
    }

    const texture_encoding = texture.encoding;

    if (texture_encoding === LinearEncoding) {
        // nothing
    } else if (texture_encoding === sRGBEncoding) {
        // convert value to linear space
        // sRGB_to_linear(out, 0, out, 0);
    }
}