const toolbarConfig = {
  minSize: 25,
  maxSize: 87,
  lensCount: 5,
  autoHide: false,
  size: 25,
  gap: 10,
  radius: 7,
  paddingY: 6,
  iconPadding: 5,
  iconSize: 13,
};

const numericSetting = (settings, key, fallback) => {
  const value = Number(settings?.[key]);
  return Number.isFinite(value) ? value : fallback;
};

const applyToolbarVars = (settings = {}) => {
  const root = document.documentElement;
  const minSize = numericSetting(settings, "toolbarMinSize", toolbarConfig.minSize);
  const maxSize = Math.max(minSize, numericSetting(settings, "toolbarMaxSize", toolbarConfig.maxSize));
  const lensCount = Math.max(1, Math.round(numericSetting(settings, "toolbarLensCount", toolbarConfig.lensCount)));
  const autoHide = settings?.toolbarAutoHide ?? toolbarConfig.autoHide;
  root.style.setProperty("--tb-size", `${minSize}px`);
  root.style.setProperty("--tb-min-size", `${minSize}px`);
  root.style.setProperty("--tb-max-size", `${maxSize}px`);
  root.style.setProperty("--tb-lens-count", `${lensCount}`);
  root.style.setProperty("--tb-autohide", autoHide ? "1" : "0");
  root.style.setProperty("--tb-gap", `${toolbarConfig.gap}px`);
  root.style.setProperty("--tb-radius", `${toolbarConfig.radius}px`);
  root.style.setProperty("--tb-pad-y", `${toolbarConfig.paddingY}px`);
  root.style.setProperty("--tb-icon-pad", `${toolbarConfig.iconPadding}px`);
  root.style.setProperty("--tb-icon-size", `${toolbarConfig.iconSize}px`);
};

export { toolbarConfig, applyToolbarVars };
