import { assert } from "../../../../../core/assert.js";
import { sampler2d_scale_down_linear } from "./sampler2d_scale_down_linear.js";
import { Sampler2D } from "../Sampler2D.js";
import { sampler2d_scale_up_linear } from "./sampler2d_scale_up_linear.js";
import { sampler2d_scale_generic } from "./sampler2d_scale_generic.js";

/**
 *
 * @param {Sampler2D} input
 * @param {Sampler2D} output
 */
export function sampler2d_scale(input, output) {
    assert.equal(input.isSampler2D, true, 'input.isSampler2D !== true');
    assert.equal(output.isSampler2D, true, 'output.isSampler2D !== true');

    const targetItemSize = output.itemSize;

    assert.equal(input.itemSize, targetItemSize, 'both source and target must have the same number of channels');

    const targetWidth = output.width;
    const targetHeight = output.height;
    const sourceWidth = input.width;
    const sourceHeight = input.height;

    if (sourceWidth === targetWidth && sourceHeight === targetHeight) {
        // same dimensions, copy directly
        output.data.set(input.data);
    } else if (sourceWidth > targetWidth && sourceHeight > targetHeight) {
        // downscaling
        if (Number.isInteger(sourceWidth / targetWidth) && Number.isInteger(sourceHeight / targetHeight)) {
            // dimensions are multiples of source/target
            sampler2d_scale_down_linear(input, output);
        } else {
            // generic downsample
            sampler2d_scale_generic(input, output);
        }
    } else if (sourceWidth < targetWidth && sourceHeight < targetHeight) {
        // upscaling
        sampler2d_scale_up_linear(input, output);
    } else {
        // mixed resampling
        sampler2d_scale_generic(input, output);
    }
}
