import { Sampler2D } from "../../../graphics/texture/sampler/Sampler2D.js";
import { sampler2d_scale_down_linear } from "../../../graphics/texture/sampler/resize/sampler2d_scale_down_linear.js";
import { computeWholeDivisorLow } from "../../../../core/math/computeWholeDivisorLow.js";

/**
 *
 * @param {Sampler2D} heightSampler
 * @returns {Sampler2D}
 */
export function buildCameraTargetSampler({ heightSampler }) {
    function s(v) {
        const divisor = computeWholeDivisorLow(v, 32);

        const i = v / divisor;

        const j = Math.ceil(i);

        return Math.max(1, j);
    }

    const result = Sampler2D.float32(1, s(heightSampler.width), s(heightSampler.height));

    sampler2d_scale_down_linear(heightSampler, result);

    return result;
}
