import { EntityInitializationCause, system, world } from "@minecraft/server";
import FSDynamicProperty from "../fs/libs/FSDynamicProperty";
import EntityManager from "../EntityManager";
import FSEntityHelper from "../fs/FSEntityHelper";
import { FSCompassDirection } from "../fs/FSCompassDirection";
const open_time_limit = 6;
export default class SecurityDoor {
    static init(entity, spawnCause) {
        if (spawnCause === EntityInitializationCause.Spawned) {
            FSEntityHelper.snapRotation(entity);
            const compass_direction = FSEntityHelper.getCompassDirection(entity);
            let event = "fs:facing_south";
            switch (compass_direction) {
                case FSCompassDirection.South:
                    event = "fs:set_facing_south";
                    break;
                case FSCompassDirection.West:
                    event = "fs:set_facing_west";
                    break;
                case FSCompassDirection.North:
                    event = "fs:set_facing_north";
                    break;
                case FSCompassDirection.East:
                    event = "fs:set_facing_east";
                    break;
                default:
                    break;
            }
            entity.triggerEvent(event);
        }
        else {
            FSEntityHelper.snapRotation(entity);
            const newLoc = EntityManager.getLocationOffsetByRotation(entity, 0.5, 0, 0);
            entity.teleport(newLoc);
            FSDynamicProperty.setBoolean(entity, "is_open", false);
            SecurityDoor.onClose(entity);
        }
    }
    static tick(entity) {
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            FSDynamicProperty.incrementNumber(entity, "open_time", 1);
            if (FSDynamicProperty.getNumber(entity, "open_time") > open_time_limit) {
                SecurityDoor.onClose(entity);
                FSDynamicProperty.setBoolean(entity, "is_open", false);
                FSDynamicProperty.setNumber(entity, "open_time", 0);
            }
        }
    }
    static onInteract(entity) {
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            SecurityDoor.onClose(entity);
            FSDynamicProperty.setBoolean(entity, "is_open", false);
        }
        else {
            SecurityDoor.onOpen(entity);
            FSDynamicProperty.setBoolean(entity, "is_open", true);
        }
    }
    static getFillArea(entity) {
        const loc0 = EntityManager.getLocationOffsetByRotation(entity, -1.5, 0, 0);
        const loc1 = EntityManager.getLocationOffsetByRotation(entity, 1.5, 3, 0);
        return `${loc0.x} ${loc0.y} ${loc0.z} ${loc1.x} ${loc1.y} ${loc1.z}`;
    }
    static onClose(entity) {
        const fill_area = SecurityDoor.getFillArea(entity);
        let command = `/fill ${fill_area} barrier [] replace air []`;
        entity.runCommand(command);
        world.playSound("fs.sbunk.elevator_close", entity.location);
        EntityManager.playAnimation(entity, "close");
    }
    static onKilled(entity) {
        const dimension = entity.dimension;
        const fill_area = SecurityDoor.getFillArea(entity);
        const command = `/fill ${fill_area} air [] replace barrier []`;
        system.run(() => {
            dimension.runCommand(command);
        });
    }
    static onOpen(entity) {
        const fill_area = SecurityDoor.getFillArea(entity);
        let command = `fill ${fill_area} air [] replace barrier []`;
        entity.runCommand(command);
        EntityManager.playAnimation(entity, "open");
        world.playSound("fs.sbunk.elevator_close", entity.location);
        FSDynamicProperty.setNumber(entity, "open_time", 0);
    }
    static onDeny(entity) {
        const fill_area = SecurityDoor.getFillArea(entity);
        let command = `fill ${fill_area} barrier [] replace air []`;
        entity.runCommand(command);
        EntityManager.playAnimation(entity, "deny");
    }
}

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