import { addTo, getGroup, getRoom, rsd } from '.';
const keyFromCoords = (coords) => {
    return `${coords[0]}_${coords[1]}_${coords[2]}`;
};
// LoopBehaviour through all rooms to define wall types between the,
const roomManager = (rooms, options) => {
    const map = {};
    const roomTypeManager = (roomData, key, index) => {
        if (!roomData.roomType) {
            roomData.roomType = rsd(options.seed).randomFromArray(['DOORS', 'WINDOWS', 'OPEN', 'CONNECTED']);
        }
        if (!roomData.wallTypes)
            roomData.wallTypes = [];
        if (map[key]) {
            // Has an adjacent rooms
            if (roomData.roomType === 'CLOSED') {
                roomData.wallTypes[index] = 'PLAIN';
            }
            else if (roomData.roomType === 'DOORS') {
                roomData.wallTypes[index] = 'DOOR';
                roomData.beenSet = true;
            }
            else if (roomData.roomType === 'CONNECTED') {
                roomData.wallTypes[index] = rsd(options.seed).randomFromArray(['DOOR', 'NONE']);
                roomData.beenSet = true;
            }
            else if (roomData.roomType === 'WINDOWS') {
                roomData.wallTypes[index] = 'WINDOW';
                roomData.beenSet = true;
            }
            else {
                roomData.wallTypes[index] = 'NONE';
            }
        }
        else {
            if (roomData.roomType === 'DOORS') {
                !roomData.beenSet
                    ? (roomData.wallTypes[index] = 'DOOR')
                    : (roomData.wallTypes[index] = rsd(options.seed).randomFromArray(['DOOR', 'WINDOW', 'PLAIN']));
                roomData.beenSet = true;
            }
            else if (roomData.roomType === 'WINDOWS') {
                !roomData.beenSet
                    ? (roomData.wallTypes[index] = 'WINDOW')
                    : (roomData.wallTypes[index] = rsd(options.seed).randomFromArray(['DOOR', 'WINDOW', 'PLAIN']));
                roomData.beenSet = true;
            }
            else {
                roomData.wallTypes[index] = 'PLAIN';
            }
        }
    };
    for (let index = 0; index < rooms.length; index++) {
        const roomProps = rooms[index];
        const key = keyFromCoords(roomProps.coords);
        map[key] = { index, coords: roomProps.coords, roomType: roomProps.roomType, wallTypes: roomProps.wallTypes };
    }
    for (const key in map) {
        const roomData = map[key];
        if (!(roomData.wallTypes && roomData.wallTypes.length > 0)) {
            // Check for adjacent rooms
            // North room
            roomTypeManager(roomData, keyFromCoords([roomData.coords[0], roomData.coords[1], roomData.coords[2] + 1]), 0);
            // East room
            roomTypeManager(roomData, keyFromCoords([roomData.coords[0] - 1, roomData.coords[1], roomData.coords[2]]), 1);
            // South room
            roomTypeManager(roomData, keyFromCoords([roomData.coords[0], roomData.coords[1], roomData.coords[2] - 1]), 2);
            // West room
            roomTypeManager(roomData, keyFromCoords([roomData.coords[0] + 1, roomData.coords[1], roomData.coords[2]]), 3);
            rooms[roomData.index].wallTypes = roomData.wallTypes;
        }
    }
};
/**
 * Returns a house complex. Good for creating multiple rooms.
 * @example
 * ```
const floor = getComplex({
  p: [0, 0.1, 10],
  dim: 5,
  height: 5,
  rooms: [
    { coords: [0, 0, 0], wallTypes: ['PLAIN', 'PLAIN', 'DOOR', 'NONE'] }, // Define wall types
    { coords: [1, 0, 0], roomType: 'CONNECTED' }, // Or randomize them using roomType
    { coords: [1, 0, 1], roomType: 'OPEN' },
    { coords: [1, 0, 2], roomType: 'CONNECTED' },
    { coords: [2, 0, 2], roomType: 'WINDOWS' },
    { coords: [2, 0, 0], roomType: 'CONNECTED' },
  ],
});
addTo(root, floor);
 * ```
 */
export const getComplex = (options = {}) => {
    const rooms = options.rooms ?? [];
    const wrapper = getGroup({ p: options.p, r: options.r, s: options.s });
    delete options.p;
    delete options.r;
    delete options.s;
    delete options.rooms;
    const dim = (options.dim || 5) * 2;
    const height = (options.height || 5) * 2;
    roomManager(rooms, options);
    for (let index = 0; index < rooms.length; index++) {
        const roomProps = rooms[index];
        const wallTypes = roomProps.wallTypes || [];
        addTo(wrapper, getRoom({
            p: [roomProps.coords[0] * dim, roomProps.coords[1] * height, roomProps.coords[2] * dim],
            wallTypes,
            ...options,
        }));
    }
    return wrapper;
};
//# sourceMappingURL=getComplex.js.map