use wasm_bindgen::prelude::*;
use serde::{Deserialize, Serialize};
use alloc::string::{String, ToString};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[wasm_bindgen]
pub struct Muhurat {
#[wasm_bindgen(getter_with_clone)]
pub name: String,
pub start: f64,
pub end: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[wasm_bindgen]
pub struct DayMuhurats {
#[wasm_bindgen(getter_with_clone)]
pub rahu_kalam: Muhurat,
#[wasm_bindgen(getter_with_clone)]
pub yamaganda: Muhurat,
#[wasm_bindgen(getter_with_clone)]
pub gulika: Muhurat,
}
#[wasm_bindgen]
pub fn calculate_muhurats(sunrise_ms: f64, sunset_ms: f64, weekday: u8) -> DayMuhurats {
let duration = sunset_ms - sunrise_ms;
let octad = duration / 8.0;
let rahu_octad = match weekday {
0 => 8, 1 => 2, 2 => 7, 3 => 5, 4 => 6, 5 => 4, 6 => 3,
_ => 8, };
let yama_octad = match weekday {
0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1, 5 => 7, 6 => 6,
_ => 5,
};
let gulika_octad = match weekday {
0 => 7, 1 => 6, 2 => 5, 3 => 4, 4 => 3, 5 => 2, 6 => 1,
_ => 7,
};
let make_muhurat = |name: &str, start_octad: u8| -> Muhurat {
let start_idx = (start_octad - 1) as f64;
let start_time = sunrise_ms + (start_idx * octad);
let end_time = start_time + octad;
Muhurat {
name: name.to_string(),
start: start_time,
end: end_time,
}
};
DayMuhurats {
rahu_kalam: make_muhurat("Rahu Kalam", rahu_octad),
yamaganda: make_muhurat("Yamaganda", yama_octad),
gulika: make_muhurat("Gulika Kalam", gulika_octad),
}
}