import { Cache } from "../../../core/cache/Cache.js";

import { strictEquals } from "../../../core/function/strictEquals.js";
import { computeStringHash } from "../../../core/primitives/strings/computeStringHash.js";
import { string_compute_byte_size } from "../../../core/primitives/strings/string_compute_byte_size.js";
import { parseTooltipString } from "./parser/parseTooltipString.js";

export class TooltipParser {


    /**
     *
     * @type {Cache<String,Token[]>}
     * @private
     */
    #cache = new Cache({
        maxWeight: 1048576,
        keyWeigher: string_compute_byte_size,
        valueWeigher(tokens) {
            return tokens.length * 256;
        },
        keyHashFunction: computeStringHash,
        keyEqualityFunction: strictEquals
    });

    /**
     *
     * @param {string} code
     * @returns {Token[]}
     */
    parse(code) {

        return this.#cache.getOrCompute(code, parseTooltipString);

    }
}
