import { ZERO } from "./parsers.js";
/* @license
 * Copyright 2019 Google LLC. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const degreesToRadians = (numberNode, fallbackRadianValue = 0) => {
  let { number, unit } = numberNode;
  if (!isFinite(number)) {
    number = fallbackRadianValue;
    unit = "rad";
  } else if (numberNode.unit === "rad" || numberNode.unit == null) {
    return numberNode;
  }
  const valueIsDegrees = unit === "deg" && number != null;
  const value = valueIsDegrees ? number : 0;
  const radians = value * Math.PI / 180;
  return { type: "number", number: radians, unit: "rad" };
};
const lengthToBaseMeters = (numberNode, fallbackMeterValue = 0) => {
  let { number, unit } = numberNode;
  if (!isFinite(number)) {
    number = fallbackMeterValue;
    unit = "m";
  } else if (numberNode.unit === "m") {
    return numberNode;
  }
  let scale;
  switch (unit) {
    default:
      scale = 1;
      break;
    case "cm":
      scale = 1 / 100;
      break;
    case "mm":
      scale = 1 / 1e3;
      break;
  }
  const value = scale * number;
  return { type: "number", number: value, unit: "m" };
};
const normalizeUnit = (() => {
  const identity = (node) => node;
  const unitNormalizers = {
    "rad": identity,
    "deg": degreesToRadians,
    "m": identity,
    "mm": lengthToBaseMeters,
    "cm": lengthToBaseMeters
  };
  return (node, fallback = ZERO) => {
    if (!isFinite(node.number)) {
      node.number = fallback.number;
      node.unit = fallback.unit;
    }
    const { unit } = node;
    if (unit == null) {
      return node;
    }
    const normalize = unitNormalizers[unit];
    if (normalize == null) {
      return fallback;
    }
    return normalize(node);
  };
})();
export {
  degreesToRadians,
  lengthToBaseMeters,
  normalizeUnit
};
//# sourceMappingURL=conversions.js.map
