/**
 * Convert the pan value that comes from a backend mixer
 * to a 0-100 % usable by a draggable panner element
 */
const convertPanToPercent = (mixerPan) => {
  const value = (((mixerPan + 90) / 90) * 100) / 2;

  if (value < 0) {
    return 0;
  } else if (value > 100) {
    return 100;
  } else {
    return value;
  }
};

/**
 * Convert the % value of a draggable panner element
 * to a mixer-ready pan value
 */
const convertPercentToPan = (percent) => {
  const value = 2 * percent / 100 * 90 - 90;

  if (value < -90) {
    return -90;
  } else if (value > 90) {
    return 90;
  } else {
    return Math.round(value);
  }
};

const convertPercentToPanForDisplay = (percent) => {
  return Math.abs(convertPercentToPan(percent));
};

const usePanHelpers = () => {
  return {
    convertPanToPercent,
    convertPercentToPan,
    convertPercentToPanForDisplay,
  };
};

export default usePanHelpers;

// Also export the functions directly for use in non-React contexts
export {
  convertPanToPercent,
  convertPercentToPan,
  convertPercentToPanForDisplay,
};
