import { EntityComponentTypes, EntityDamageCause, EntityRideableComponent, Player, system, } from "@minecraft/server";
import { entityDamageConfig } from "../DATA/DamageConfig";
import { getLinkedPlayer } from "./Linking";
import * as Vector3Utils from "../U/Vector3Utils";
import MinecraftBlockNonSolidTypes from "../E/MinecraftBlockNonSolidTypes";
let entityHit;
function isMountIdSpectral(mountEntityId) {
    return mountEntityId.startsWith("kicu_mmad:spectral_");
}
export function shadowPhoenixAttack(mountEntity) {
    const rider = getLinkedPlayer(mountEntity);
    const shadowPhoenixConfig = entityDamageConfig[mountEntity.typeId];
    const shadowPhoenix = shadowPhoenixConfig.shadowPhoenix;
    if (rider && shadowPhoenix) {
        const riderViewDirection = rider.getViewDirection();
        const riderBlockRaycastHit = rider.getBlockFromViewDirection({
            maxDistance: shadowPhoenix.teleportMaxDistance,
        });
        const riderEntityRaycastHit = rider.getEntitiesFromViewDirection({
            maxDistance: shadowPhoenix.teleportMaxDistance,
        });
        entityHit = riderEntityRaycastHit[0]?.entity;
        if (entityHit?.id === mountEntity?.id)
            entityHit = riderEntityRaycastHit[1]?.entity;
        let teleportLocation = undefined;
        if (riderBlockRaycastHit || entityHit) {
            if (entityHit) {
                teleportLocation = Vector3Utils.add(entityHit.location, Vector3Utils.multiply(riderViewDirection, -2));
            }
            else if (riderBlockRaycastHit) {
                const hitLocation = Vector3Utils.add(riderBlockRaycastHit.faceLocation, riderBlockRaycastHit.block.location);
                teleportLocation = Vector3Utils.add(hitLocation, Vector3Utils.multiply(riderViewDirection, -2));
            }
        }
        let hasTeleported = false;
        if (teleportLocation) {
            if (isSafeToTeleportLocation(teleportLocation, mountEntity, rider)) {
                const now = Date.now();
                const cooldownTime = (mountEntity.getDynamicProperty("kicu_mmad:shadow_phoenix_cooldown") ??
                    0);
                if (now >= cooldownTime) {
                    mountEntity.setDynamicProperty("kicu_mmad:shadow_phoenix_cooldown", now + shadowPhoenix.teleportCooldown * 1000);
                    drawLineParticle(mountEntity, teleportLocation);
                    mountEntity.teleport(teleportLocation);
                    hasTeleported = true;
                    shadowPhoenixRadiusAttack(mountEntity);
                }
                else {
                    const remainingCooldown = (cooldownTime - now) / 1000;
                    const cooldownTimeInSeconds = remainingCooldown.toFixed(1);
                    const isCooldown = remainingCooldown > 0;
                    if (isCooldown && remainingCooldown >= 0.1) {
                        const sourceRideable = mountEntity.getComponent(EntityRideableComponent.componentId);
                        if (sourceRideable) {
                            const riders = sourceRideable.getRiders();
                            for (let k = 0; k < riders.length; k++) {
                                const player = riders[k];
                                if (player instanceof Player) {
                                    player.onScreenDisplay.setActionBar({
                                        rawtext: [
                                            {
                                                translate: "dialog.kicu_mmad.shadowphoenixtimer",
                                                with: [`\n`, `${cooldownTimeInSeconds}s`],
                                            },
                                        ],
                                    });
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!hasTeleported && riderBlockRaycastHit === undefined) {
            const teleportLocation = Vector3Utils.add(mountEntity.location, Vector3Utils.multiply(riderViewDirection, shadowPhoenix.teleportMaxDistance));
            if (isSafeToTeleportLocation(teleportLocation, mountEntity, rider)) {
                const now = Date.now();
                const cooldownTime = (mountEntity.getDynamicProperty("kicu_mmad:shadow_phoenix_cooldown") ??
                    0);
                if (now >= cooldownTime) {
                    mountEntity.setDynamicProperty("kicu_mmad:shadow_phoenix_cooldown", now + shadowPhoenix.teleportCooldown * 1000);
                    drawLineParticle(mountEntity, teleportLocation);
                    mountEntity.teleport(teleportLocation);
                    shadowPhoenixRadiusAttack(mountEntity);
                }
                else {
                    const remainingCooldown = (cooldownTime - now) / 1000;
                    const cooldownTimeInSeconds = remainingCooldown.toFixed(1);
                    const isCooldown = remainingCooldown > 0;
                    if (isCooldown && remainingCooldown >= 0.1) {
                        const sourceRideable = mountEntity.getComponent(EntityRideableComponent.componentId);
                        if (sourceRideable) {
                            const riders = sourceRideable.getRiders();
                            for (let k = 0; k < riders.length; k++) {
                                const player = riders[k];
                                if (player instanceof Player) {
                                    player.onScreenDisplay.setActionBar({
                                        rawtext: [
                                            {
                                                translate: "dialog.kicu_mmad.shadowphoenixtimer",
                                                with: [`\n`, `${cooldownTimeInSeconds}s`],
                                            },
                                        ],
                                    });
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    shadowPhoenixRadiusAttack(mountEntity);
}
function isSafeToTeleportLocation(location, mount, rider) {
    const riderTeleportLocation = Vector3Utils.add(Vector3Utils.subtract(rider.location, mount.location), location);
    const riderHeadTeleportLocation = Vector3Utils.add(Vector3Utils.subtract(rider.getHeadLocation(), mount.location), location);
    const mountLocationBlock = mount.dimension.getBlock(location);
    const riderLocationBlock = rider.dimension.getBlock(riderTeleportLocation);
    const riderHeadLocationBlock = rider.dimension.getBlock(riderHeadTeleportLocation);
    const nonSolidBlockTypes = Object.values(MinecraftBlockNonSolidTypes);
    if (nonSolidBlockTypes.includes(mountLocationBlock?.typeId) &&
        nonSolidBlockTypes.includes(riderLocationBlock?.typeId) &&
        nonSolidBlockTypes.includes(riderHeadLocationBlock?.typeId)) {
        return true;
    }
    return false;
}
function shadowPhoenixRadiusAttack(mountEntity) {
    const rider = getLinkedPlayer(mountEntity);
    const shadowPhoenixConfig = entityDamageConfig[mountEntity.typeId];
    if (shadowPhoenixConfig && rider) {
        const damageLocation = Vector3Utils.add(mountEntity.location, mountEntity.getViewDirection());
        const entities = mountEntity.dimension.getEntities({
            maxDistance: shadowPhoenixConfig.skillAttackRadius,
            location: damageLocation,
        });
        for (let i = 0; i < entities.length; i++) {
            const entity = entities[i];
            if (entity.id === mountEntity.id)
                continue;
            if (entity.id === rider.id)
                continue;
            if (!entity.hasComponent(EntityComponentTypes.Health))
                continue;
            entity.applyDamage(shadowPhoenixConfig.skillDamage, {
                cause: EntityDamageCause.entityAttack,
                damagingEntity: mountEntity,
            });
        }
        system.runTimeout(() => {
            try {
                let particleId = isMountIdSpectral(mountEntity.typeId) ? "kicu_mmad:dark_impact2" : "kicu_mmad:dark_impact";
                mountEntity.dimension.spawnParticle(particleId, {
                    x: damageLocation.x,
                    y: damageLocation.y,
                    z: damageLocation.z,
                });
                const { x: vdx, y: vdy, z: vdz } = mountEntity.getViewDirection();
                const offSetLocValue = 1.5;
                const particleMoreLocation = Vector3Utils.add(damageLocation, {
                    x: vdx * offSetLocValue,
                    y: vdy + offSetLocValue / 1.25,
                    z: vdz * offSetLocValue,
                });
                mountEntity.dimension.spawnParticle(particleId, {
                    x: particleMoreLocation.x,
                    y: particleMoreLocation.y,
                    z: particleMoreLocation.z,
                });
            }
            catch { }
        }, 1);
    }
}
function drawLineParticle(entity, effectLocation) {
    let particleId = isMountIdSpectral(entity.typeId) ? "kicu_mmad:dark_tp_mount2" : "kicu_mmad:dark_tp_mount";
    const particleSpacing = 0.5;
    const maxParticles = 100;
    const dimension = entity.dimension;
    const headLoc = entity.getHeadLocation();
    const entityYHeight = Math.abs(headLoc.y - entity.location.y) / 2;
    const startPosition = {
        x: entity.location.x + 0.5,
        y: entity.location.y + entityYHeight,
        z: entity.location.z + 0.5,
    };
    const targetPosition = {
        x: effectLocation.x + 0.5,
        y: effectLocation.y + 0.75,
        z: effectLocation.z + 0.5,
    };
    const dx = targetPosition.x - startPosition.x;
    const dy = targetPosition.y - startPosition.y;
    const dz = targetPosition.z - startPosition.z;
    const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
    const particleCount = Math.min(Math.floor(distance / particleSpacing), maxParticles);
    if (particleCount <= 0)
        return;
    for (let i = 0; i <= particleCount; i++) {
        const t = i / particleCount;
        const interpolatedPosition = {
            x: startPosition.x + dx * t,
            y: startPosition.y + dy * t,
            z: startPosition.z + dz * t,
        };
        dimension.spawnParticle(particleId, interpolatedPosition);
    }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/SPXAttack.js.map
