import { EntityInitializationCause, system, world } from "@minecraft/server";
import FSEntityHelper from "../fs/FSEntityHelper";
import FSLocalizedString from "../fs/FSLocalizedString";
import FSDynamicProperty from "../fs/libs/FSDynamicProperty";
import FSEntityPerms from "../fs/libs/perms/FSEntityPerms";
import EntityManager from "../EntityManager";
import FSUtil from "../fs/FSUtil";
import FSWorldHelper from "../fs/FSWorldHelper";
const AUTOLOCK_TIME = 5;
const AUTOLOCK_PROPERTY = "autolock_time";
const TAG_UNLOCKED = FSUtil.namespacedStringCr("unlocked");
export default class LockedBlock {
    static init(entity, spawnCause) {
        if (spawnCause === EntityInitializationCause.Spawned) {
            LockedBlock.onLock(entity);
            LockedBlock.validateLock(entity);
        }
    }
    static lockBlock(block, player) {
        if (block.permutation.matches("minecraft:chest") || block.permutation.matches("minecraft:trapped_chest") || block.permutation.matches("minecraft:ender_chest")) {
            if (LockedBlock.isBlockAlreadyLocked(block)) {
                return;
            }
            const dimension = block.dimension;
            const entity = dimension.spawnEntity("57d_sbunk:locked_chest", block.bottomCenter());
            LockedBlock.onLock(entity);
            if (player) {
                FSDynamicProperty.setBoolean(player, "has_locked_chest", true);
            }
        }
    }
    static lockOtherBlock(block, otherLock) {
        if (block.permutation.matches("minecraft:chest") || block.permutation.matches("minecraft:trapped_chest") || block.permutation.matches("minecraft:ender_chest")) {
            if (LockedBlock.isBlockAlreadyLocked(block)) {
                return;
            }
            const dimension = block.dimension;
            const entity = dimension.spawnEntity("57d_sbunk:locked_chest<fs:locked>", block.bottomCenter());
            FSDynamicProperty.setString(entity, "linked_lock_id", otherLock.id);
            FSDynamicProperty.setString(otherLock, "linked_lock_id", entity.id);
            LockedBlock.onLock(entity);
            LockedBlock.updateLinkedLockPermissions(otherLock);
        }
    }
    static updateLinkedLockPermissions(entity) {
        const linked_lock = LockedBlock.findLinkedLock(entity);
        if (!linked_lock) {
            return;
        }
        let accessLevel = FSEntityPerms.getEntityPermission(entity);
        FSEntityPerms.setEntityCustomName(linked_lock, FSEntityPerms.getEntityCustomName(entity, false, false));
        FSEntityPerms.setEntityPermission(linked_lock, accessLevel);
        linked_lock.nameTag = FSEntityPerms.getEntityCustomName(entity);
        EntityManager.triggerAccessLevelEvent(linked_lock, accessLevel);
    }
    static findLinkedLock(entity) {
        const linked_lock_id = FSDynamicProperty.getString(entity, "linked_lock_id");
        if (linked_lock_id.length > 0) {
            return world.getEntity(linked_lock_id);
        }
        return undefined;
    }
    static isBlockAlreadyLocked(block) {
        const dimension = block.dimension;
        const eqo_locked_block = {
            location: block.bottomCenter(),
            type: "57d_sbunk:locked_chest",
            maxDistance: 0.25
        };
        const nearbyLockedBlocks = dimension.getEntities(eqo_locked_block);
        if (nearbyLockedBlocks.length > 0) {
            return true;
        }
        return false;
    }
    static onInteract(entity) {
        if (FSDynamicProperty.getBoolean(entity, "is_locked")) {
            LockedBlock.onUnlock(entity);
        }
        else {
            LockedBlock.onLock(entity);
        }
    }
    static validateLock(entity) {
        if (!entity.isValid()) {
            return;
        }
        const entityTypeId = entity.typeId;
        if (entityTypeId === "57d_sbunk:locked_chest") {
            let valid_block_found = false;
            let block_found = true;
            const block = FSEntityHelper.getBlockAtEntity(entity);
            if (!block) {
                block_found = false;
            }
            if (block && (block.permutation.matches("minecraft:chest") || block.permutation.matches("minecraft:trapped_chest") || block.permutation.matches("minecraft:ender_chest"))) {
                valid_block_found = true;
                const inventory = block.getComponent("inventory");
                if (inventory && inventory.container && inventory.container.size === 27) {
                }
                else if (inventory && inventory.container && inventory.container.size === 54) {
                    let blocks = [];
                    let other_block = block.north(1);
                    if (other_block) {
                        blocks.push(other_block);
                    }
                    other_block = block.east(1);
                    if (other_block) {
                        blocks.push(other_block);
                    }
                    other_block = block.south(1);
                    if (other_block) {
                        blocks.push(other_block);
                    }
                    other_block = block.west(1);
                    if (other_block) {
                        blocks.push(other_block);
                    }
                    let valid_blocks = [];
                    blocks.forEach(other_block => {
                        if (other_block && (other_block.permutation.matches("minecraft:chest") || other_block.permutation.matches("minecraft:trapped_chest") || other_block.permutation.matches("minecraft:ender_chest"))) {
                            const inventory = other_block.getComponent("inventory");
                            if (inventory && inventory.container && inventory.container.size === 54) {
                                valid_blocks.push(other_block);
                            }
                        }
                    });
                    if (valid_blocks.length === 0) {
                    }
                    else if (valid_blocks.length === 1) {
                        LockedBlock.lockOtherBlock(valid_blocks[0], entity);
                    }
                    else {
                    }
                }
            }
            if (block_found && !valid_block_found) {
                entity.remove();
            }
        }
    }
    static tickAll() {
        const eqo_locked_blocks = {
            type: "57d_sbunk:locked_chest",
            tags: [TAG_UNLOCKED]
        };
        const locked_blocks = FSWorldHelper.getEntitiesInActiveDimensions(eqo_locked_blocks);
        locked_blocks.forEach(entity => {
            LockedBlock.onTick(entity);
        });
        const eqo_locked_blocks_all = {
            type: "57d_sbunk:locked_chest"
        };
        const locked_blocks_all = FSWorldHelper.getEntitiesInActiveDimensions(eqo_locked_blocks_all);
        locked_blocks_all.forEach(entity => {
            LockedBlock.validateLock(entity);
        });
    }
    static onTick(entity) {
        if (!FSDynamicProperty.getBoolean(entity, "is_locked")) {
            const autolock_time = FSDynamicProperty.getNumber(entity, AUTOLOCK_PROPERTY);
            if (autolock_time > 0) {
                FSDynamicProperty.setNumber(entity, AUTOLOCK_PROPERTY, autolock_time - 1);
            }
            else {
                LockedBlock.onLock(entity);
            }
        }
    }
    static onLock(entity) {
        entity.removeTag(TAG_UNLOCKED);
        FSDynamicProperty.setBoolean(entity, "is_locked", true);
        entity.triggerEvent("fs:locked");
        const message = {
            rawtext: [
                { text: "§a[!] " },
                {
                    translate: FSLocalizedString.localize("locked_block.chest.locked", "Chest Locked", "Message to inform the player that the chest is locked.")
                }
            ]
        };
        world.playSound("57d_sbunk.locked", entity.location);
        world.sendMessage(message);
        const linked_lock = LockedBlock.findLinkedLock(entity);
        if (linked_lock) {
            FSDynamicProperty.setBoolean(linked_lock, "is_locked", true);
            linked_lock.triggerEvent("fs:locked");
        }
    }
    static onUnlock(entity) {
        FSDynamicProperty.setBoolean(entity, "is_locked", false);
        entity.triggerEvent("fs:unlocked");
        const message = {
            rawtext: [
                { text: "§c[!] " },
                {
                    translate: FSLocalizedString.localize("locked_block.chest.unlocked", "Chest Unlocked - Interact with the chest again to open it. The chest will automatically lock again in 5 seconds.", "Message to inform the player that the chest is unlocked.")
                }
            ]
        };
        world.playSound("57d_sbunk.unlocked", entity.location);
        world.sendMessage(message);
        FSDynamicProperty.setNumber(entity, AUTOLOCK_PROPERTY, AUTOLOCK_TIME);
        entity.addTag(TAG_UNLOCKED);
        const linked_lock = LockedBlock.findLinkedLock(entity);
        if (linked_lock) {
            FSDynamicProperty.setBoolean(linked_lock, "is_locked", false);
            linked_lock.triggerEvent("fs:unlocked");
        }
    }
    static onDeny(entity) {
        entity.triggerEvent("fs:locked");
    }
    static onKilled(entity) {
        const linked_lock = LockedBlock.findLinkedLock(entity);
        system.run(() => {
            if (linked_lock && linked_lock.isValid()) {
                linked_lock.remove();
            }
        });
    }
}

//# sourceMappingURL=../../../../_SBUNK_BPDebug/fs_sbunk/entity/LockedBlock.js.map
