// engine.mjs: the arithmetic behind /strata/what-is-umami/. Everything the page draws comes from here,
// and verify-what-is-umami.mjs imports this same file.
//
// Yamaguchi (1967) fitted her panel's matches of glutamate + inosinate mixtures to MSG alone with
//   y = u + GAMMA * u * v
// u = MSG, v = IMP, y = the MSG concentration that tastes as strong as the mixture, all in g/100 ml.

export const GAMMA_IMP = 1218;

// Yamaguchi's IMP was the disodium salt with 7.5 waters (the 1967 summary says so), so v is grams of that.
// Na2IMP = C10H11N4Na2O8P, 392.17 g/mol; plus 7.5 H2O (18.015 each) = 527.28 g/mol.
export const MW_IMP_HYDRATE = 527.28;
/** Millimolar IMP to Yamaguchi's v (g of the 7.5-hydrate per 100 ml). */
export const mMToV = (mM) => (mM / 1000) * MW_IMP_HYDRATE / 10;

// The law in the three forms it is printed in. Only the first is the one anyone computes with.
export const LAWS = {
  used: { label: 'y = u + 1218 u v', f: (u, v) => u + 1218 * u * v },
  jnutr2000: { label: 'y = u + 1218 v, as printed in J. Nutr. 2000', f: (u, v) => u + 1218 * v },
  record1967: { label: 'y = u + 1.218 × 10⁸ u v, as the 1967 summary’s online record reads', f: (u, v) => u + 1.218e8 * u * v },
};

/** How far glutamate's taste threshold falls with IMP at v: y/u at any u, which is 1 + gamma v. */
export const thresholdDrop = (v, gamma = GAMMA_IMP) => 1 + gamma * v;

/** Equivalent MSG concentration of a mixture (g/100 ml). */
export const equivalent = (u, v, gamma = GAMMA_IMP) => u + gamma * u * v;

/** How many times stronger the mixture is than the same total weight of MSG alone. */
export const multiplier = (u, v, gamma = GAMMA_IMP) => (u + v > 0 ? equivalent(u, v, gamma) / (u + v) : 1);

/** A fixed total c split with a fraction f of it as IMP. */
export const split = (c, f, gamma = GAMMA_IMP) => {
  const v = c * f, u = c - v;
  return { u, v, y: equivalent(u, v, gamma), times: multiplier(u, v, gamma) };
};

/** The split of a fixed total c that tastes strongest: dy/du = 0 gives u = (1 + gamma c) / (2 gamma). */
export const bestSplit = (c, gamma = GAMMA_IMP) => {
  const u = Math.min(c, (1 + gamma * c) / (2 * gamma));
  const f = 1 - u / c;
  return { f, ...split(c, f, gamma) };
};

// The same shape from a receptor. A standard allosteric (ternary-complex) model: glutamate G binds with
// dissociation constant Kg, the nucleotide I binds a second site with Ki, and each makes the other's
// binding alpha times tighter. If only glutamate-bound receptor signals (Li et al. 2002: IMP alone did
// not activate human T1R1/T1R3), the signalling fraction is
//   occ = (g (1 + alpha i)) / (1 + g + i + alpha g i),   g = [G]/Kg, i = [I]/Ki.
// This is our algebra, not a fit to anyone's data; the page uses it only to show where a product term
// comes from and where it must stop.
export const occupancy = (g, i, alpha) => (g * (1 + alpha * i)) / (1 + g + i + alpha * g * i);

/** Fold shift of glutamate's half-maximal concentration produced by the nucleotide at level i. */
export const ec50Shift = (i, alpha) => (1 + alpha * i) / (1 + i);

/** The first year after which series b is ahead of series a in every remaining year. */
export function crossover(a, b, yearStart = 1900) {
  let last = -1;
  for (let i = 0; i < a.length; i++) if (b[i] <= a[i]) last = i;
  return yearStart + last + 1;
}
