use anyhow::Result;
use derive_docs::stdlib;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
    errors::KclError,
    executor::MemoryItem,
    std::{Args, SketchGroup, SketchSurface},
};
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(untagged)]
pub enum SketchSurfaceOrGroup {
    SketchSurface(SketchSurface),
    SketchGroup(Box<SketchGroup>),
}
pub async fn circle(args: Args) -> Result<MemoryItem, KclError> {
    let (center, radius, sketch_surface_or_group, tag): ([f64; 2], f64, SketchSurfaceOrGroup, Option<String>) =
        args.get_circle_args()?;
    let sketch_group = inner_circle(center, radius, tag, sketch_surface_or_group, args).await?;
    Ok(MemoryItem::SketchGroup(sketch_group))
}
#[stdlib {
    name = "circle",
}]
async fn inner_circle(
    center: [f64; 2],
    radius: f64,
    tag: Option<String>,
    sketch_surface_or_group: SketchSurfaceOrGroup,
    args: Args,
) -> Result<Box<SketchGroup>, KclError> {
    let sketch_surface = match sketch_surface_or_group {
        SketchSurfaceOrGroup::SketchSurface(surface) => surface,
        SketchSurfaceOrGroup::SketchGroup(group) => group.on,
    };
    let mut sketch_group =
        crate::std::sketch::inner_start_profile_at([center[0] + radius, center[1]], sketch_surface, None, args.clone())
            .await?;
    sketch_group = crate::std::sketch::inner_arc(
        crate::std::sketch::ArcData::AnglesAndRadius {
            angle_start: 0.0,
            angle_end: 360.0,
            radius,
        },
        sketch_group,
        tag,
        args.clone(),
    )
    .await?;
    crate::std::sketch::inner_close(sketch_group, None, args).await
}