import { m as macro } from '../../../macros2.js';
import { r as radiansFromDegrees } from '../../../Common/Core/Math/index.js';
import vtkDracoReader from '../DracoReader.js';
import vtkLight from '../../../Rendering/Core/Light.js';
import { MIN_LIGHT_ATTENUATION } from './Constants.js';

const {
  vtkWarningMacro
} = macro;

/**
 * Handles the KHR_materials_unlit extension.
 *
 * @param {object} extension - The KHR_materials_unlit extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
function handleKHRMaterialsUnlit(extension, property) {
  property.setLighting(true);
}

/**
 * Handles the KHR_materials_ior extension.
 *
 * @param {object} extension - The KHR_materials_unlit extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
function handleKHRMaterialsIor(extension, property) {
  property.setBaseIOR(extension.ior);
}

/**
 * Handles the KHR_materials_specular extension.
 * @param {object} extension - The KHR_materials_specular extension object.
 * @param {vtkProperty} property - The vtkProperty instance to update.
 */
function handleKHRMaterialsSpecular(extension, property) {
  property.setSpecular(extension.specularFactor);
  property.setSpecularColor(extension.specularColorFactor);
}

/**
 * Handles the KHR_lights_punctual extension.
 *
 * @param {object} extension - The KHR_lights_punctual extension object.
 * @param {vtkRenderer} renderer - The vtkRenderer instance to add the light to.
 */
function handleKHRLightsPunctual(extension, transformMatrix, model) {
  const {
    light
  } = extension;
  const {
    color,
    intensity,
    range,
    spot,
    type
  } = light;
  const l = vtkLight.newInstance({
    color: color || [1, 1, 1],
    intensity: intensity || 1.0
  });

  // Apply the global transform to the light
  l.setTransformMatrix(transformMatrix);

  // Handle range
  if (range > 0) {
    // Set quadratic values to get attenuation(range) ~= MIN_LIGHT_ATTENUATION
    l.setAttenuationValues(1, 0, 1.0 / (range * range * MIN_LIGHT_ATTENUATION));
  }
  switch (type) {
    case 'directional':
      l.setPositional(false);
      break;
    case 'point':
      l.setPositional(true);
      l.setConeAngle(90);
      break;
    case 'spot':
      l.setPositional(true);
      l.setConeAngle(radiansFromDegrees(spot.outerConeAngle));
      break;
    default:
      vtkWarningMacro(`Unsupported light type: ${type}`);
  }
  model.lights.set(light.name, l);
}

/**
 * Handles the KHR_draco_mesh_compression extension.
 *
 * @param {object} extension - The KHR_draco_mesh_compression extension object.
 */
function handleKHRDracoMeshCompression(extension) {
  const reader = vtkDracoReader.newInstance();
  reader.parse(extension.bufferView);
  return reader.getOutputData();
}

/**
 * Handles the KHR_materials_variants extension.
 *
 * @param {object} extension - The KHR_materials_variants extension object.
 * @param {object} model - The model object to update with variant information.
 */
function handleKHRMaterialsVariants(extension, model) {
  model.variants = extension.variants.map(v => v.name);
}

export { handleKHRDracoMeshCompression, handleKHRLightsPunctual, handleKHRMaterialsIor, handleKHRMaterialsSpecular, handleKHRMaterialsUnlit, handleKHRMaterialsVariants };
