import { split_by_2 } from "../../../../../core/binary/split_by_2.js";
import { compose_finger_print } from "./compose_finger_print.js";

/**
 *
 * @param {number} index
 * @returns {number}
 */
export function tile_address_to_finger_print(index) {
    for (let mip = 0; mip < 99; mip++) {
        const resolution = 1 << mip;

        const mip_mask = resolution - 1;

        const index_offset = split_by_2(mip_mask);

        const next_mip_mask = mip_mask << 1 | 0x1;

        const index_offset_next = split_by_2(next_mip_mask);

        if (index >= index_offset_next) {
            continue;
        }

        const local_index = index - index_offset;

        const y = (local_index / resolution) | 0;
        const x = local_index % resolution;

        return compose_finger_print(mip, x, y,);
    }

    // not found
    return -1;
}