use wasm_bindgen::prelude::*;
use serde::{Serialize, Deserialize};
use alloc::vec::Vec;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[wasm_bindgen(getter_with_clone)]
pub struct HouseInfo {
pub ascendant: f64,
pub mc: f64,
pub armc: f64,
pub vertex: f64,
pub equatorial_ascendant: f64,
pub co_ascendant1: f64,
pub co_ascendant2: f64,
pub polar_ascendant: f64,
pub cusps: Vec<f64>,
}
use crate::astronomy::ayanamsha::{AyanamshaMode, get_ayanamsha};
pub fn calculate_houses(
jd: f64,
lat: f64,
lon: f64,
hsys: char,
ayan_mode: Option<AyanamshaMode>
) -> Result<HouseInfo, JsValue> {
let sys = match hsys {
'P' => swiss_eph::safe::HouseSystem::Placidus,
'K' => swiss_eph::safe::HouseSystem::Koch,
'O' => swiss_eph::safe::HouseSystem::Porphyrius,
'R' => swiss_eph::safe::HouseSystem::Regiomontanus,
'C' => swiss_eph::safe::HouseSystem::Campanus,
'E' => swiss_eph::safe::HouseSystem::Equal,
'W' => swiss_eph::safe::HouseSystem::WholeSign,
'B' => swiss_eph::safe::HouseSystem::Alcabitus,
'M' => swiss_eph::safe::HouseSystem::Morinus,
'T' => swiss_eph::safe::HouseSystem::Topocentric,
'V' => swiss_eph::safe::HouseSystem::Vehlow,
_ => swiss_eph::safe::HouseSystem::Placidus, };
let mut houses_res = swiss_eph::safe::houses(jd, lat, lon, sys);
if houses_res.is_err() {
houses_res = swiss_eph::safe::houses(jd, lat, lon, swiss_eph::safe::HouseSystem::Porphyrius);
}
if houses_res.is_err() {
houses_res = swiss_eph::safe::houses(jd, lat, lon, swiss_eph::safe::HouseSystem::Equal);
}
let houses = houses_res.map_err(|e| JsValue::from_str(&e.message))?;
let mut asc_deg = houses.ascendant;
let mut mc_deg = houses.mc;
let armc = houses.armc;
let mut vertex = houses.vertex;
let eq_asc = houses.equatorial_ascendant;
let co_asc1 = houses.co_ascendant_koch;
let co_asc2 = houses.co_ascendant_munkasey;
let pol_asc = houses.polar_ascendant;
let mut cusps_vec = houses.cusps.to_vec();
if let Some(mode) = ayan_mode {
let ayan = get_ayanamsha(mode, jd);
asc_deg = (asc_deg - ayan + 360.0) % 360.0;
mc_deg = (mc_deg - ayan + 360.0) % 360.0;
vertex = (vertex - ayan + 360.0) % 360.0;
match hsys {
'W' => {
let sign_start = (asc_deg / 30.0).floor() * 30.0;
for i in 0..12 {
cusps_vec[i] = (sign_start + (i as f64) * 30.0) % 360.0;
}
},
'E' => {
for i in 0..12 {
cusps_vec[i] = (asc_deg + (i as f64) * 30.0) % 360.0;
}
},
_ => {
for i in 0..12 {
cusps_vec[i] = (cusps_vec[i] - ayan + 360.0) % 360.0;
}
}
}
}
Ok(HouseInfo {
ascendant: asc_deg,
mc: mc_deg,
armc,
vertex,
equatorial_ascendant: eq_asc,
co_ascendant1: co_asc1,
co_ascendant2: co_asc2,
polar_ascendant: pol_asc,
cusps: cusps_vec,
})
}