// Copyright © 2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

use microcad_lang::{builtin::*, model::*};

/// Builtin definition to orient an object towards an axis.
#[derive(Debug)]
pub struct Orient;

impl BuiltinWorkbenchDefinition for Orient {
    fn id() -> &'static str {
        "orient"
    }

    fn kind() -> BuiltinWorkbenchKind {
        BuiltinWorkbenchKind::Transform
    }

    fn workpiece_function() -> &'static BuiltinWorkpieceFn {
        use microcad_core::Vec3;
        &|args| {
            Ok(BuiltinWorkpieceOutput::Transform(
                AffineTransform::Rotation(crate::math::orient_z_to(Vec3::new(
                    args.get("x"),
                    args.get("y"),
                    args.get("z"),
                ))),
            ))
        }
    }

    fn parameters() -> ParameterValueList {
        [
            parameter!(x: Scalar),
            parameter!(y: Scalar),
            parameter!(z: Scalar),
        ]
        .into_iter()
        .collect()
    }
}
