import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
export class SearchComponent extends utils.ActorComponent {
    async searchForBlock(dimension, origin, searchSize, blockName, states) {
        let jobId = 0;
        const block = await new Promise((resolve) => {
            function* searchGenerator(dimension, origin, searchSize, blockName, states) {
                for (let cube_size = 0; cube_size < searchSize; cube_size++) {
                    for (let y = -cube_size; y <= cube_size; y++) {
                        for (let x = -cube_size; x <= cube_size; x++) {
                            for (let z = -cube_size; z <= cube_size; z++) {
                                if (Math.abs(x) === cube_size || Math.abs(y) === cube_size || Math.abs(z) === cube_size) {
                                    try {
                                        const block = dimension.getBlock(origin.add({ x, y, z }));
                                        if (block === null || block === void 0 ? void 0 : block.matches(blockName, states)) {
                                            resolve(block);
                                        }
                                    }
                                    catch (error) {
                                        utils.mcerror(error);
                                        resolve(undefined);
                                    }
                                    yield;
                                }
                            }
                        }
                    }
                }
                resolve(undefined);
            }
            jobId = mc.system.runJob(searchGenerator(dimension, origin, searchSize, blockName, states));
        });
        mc.system.clearJob(jobId);
        return block;
    }
    async iterateOverBlocks(dimension, origin, searchSize, callback) {
        let jobId = 0;
        const block = await new Promise((resolve) => {
            function* searchGenerator(dimension, origin, searchSize, callback) {
                for (let cube_size = 0; cube_size < searchSize; cube_size++) {
                    for (let y = -cube_size; y <= cube_size; y++) {
                        for (let x = -cube_size; x <= cube_size; x++) {
                            for (let z = -cube_size; z <= cube_size; z++) {
                                if (Math.abs(x) === cube_size || Math.abs(y) === cube_size || Math.abs(z) === cube_size) {
                                    try {
                                        const block = dimension.getBlock(origin.add({ x, y, z }));
                                        if (block === null || block === void 0 ? void 0 : block.isValid()) {
                                            if (callback(block))
                                                resolve();
                                        }
                                    }
                                    catch (error) {
                                        utils.mcerror(error);
                                    }
                                    yield;
                                }
                            }
                        }
                    }
                }
                resolve(undefined);
            }
            jobId = mc.system.runJob(searchGenerator(dimension, origin, searchSize, callback));
        });
        mc.system.clearJob(jobId);
        return block;
    }
}
