///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

// Model units for the user to choose from.

export const ModelUnits = {
  Meters: { name: "Meters", type: "m", scale: 1.0 },
  Centimeters: { name: "Centimeters", type: "cm", scale: 0.01 },
  Millimeters: { name: "Millimeters", type: "mm", scale: 0.001 },
  Feet: { name: "Feet", type: "ft", scale: 0.3048 },
  Inches: { name: "Inches", type: "in", scale: 0.0254 },
  Yards: { name: "Yards", type: "yd", scale: 0.9144 },
  Kilometers: { name: "Kilometers", type: "km", scale: 1000.0 },
  Miles: { name: "Miles", type: "mi", scale: 1609.344 },
  Micrometers: { name: "Micrometers", type: "µm", scale: 0.000001 },
  Mils: { name: "Mils", type: "mil", scale: 0.0000254 },
  MicroInches: { name: "Micro-inches", type: "µin", scale: 0.0000000254 },
  Default: { name: "File units", type: "unit", scale: 1.0 },
};

// Convert distance from unit to unit.

export function convertUnits(fromUnits: string, toUnits: string, distance: number) {
  const fromFactor = 1 / (ModelUnits[fromUnits] || ModelUnits.Default).scale;
  const toFactor = (ModelUnits[toUnits] || ModelUnits.Default).scale || 1;
  return distance * fromFactor * toFactor;
}
