import * as Y from 'yjs';

export const randomColor = () => {
  const randomInt = (min: number, max: number) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };

  const h = randomInt(0, 360); // hue
  const s = randomInt(42, 98); // saturation
  const l = randomInt(70, 90); // lightness

  return hslToHex(h, s, l);
};

function hslToHex(h: number, s: number, l: number) {
  l /= 100;
  const a = (s * Math.min(l, 1 - l)) / 100;
  const f = (n: number) => {
    const k = (n + h / 30) % 12;
    const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
    return Math.round(255 * color)
      .toString(16)
      .padStart(2, '0');
  };
  return `#${f(0)}${f(8)}${f(4)}`;
}

export const toBase64 = (
  str: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>,
) => Buffer.from(str).toString('base64');

type BasicBlock = {
  type: string;
  content: string;
};
export const blocksToYDoc = (blocks: BasicBlock[], doc: Y.Doc) => {
  const xmlFragment = doc.getXmlFragment('document-store');

  blocks.forEach((block) => {
    const xmlElement = new Y.XmlElement(block.type);
    if (block.content) {
      xmlElement.insert(0, [new Y.XmlText(block.content)]);
    }

    xmlFragment.push([xmlElement]);
  });
};
