import { funPrefixNamer } from "../../Naming.js";
import { allLowerWordStyle, allUpperWordStyle, combineWords, firstUpperWordStyle, isAscii, isLetterOrUnderscore, isLetterOrUnderscoreOrDigit, legalizeCharacters, splitIntoWords, } from "../../support/Strings.js";
import { UnionType } from "../../Type/index.js";
import { nullableFromUnion } from "../../Type/TypeUtils.js";
const legalizeName = legalizeCharacters((cp) => isAscii(cp) && isLetterOrUnderscoreOrDigit(cp));
function elmNameStyle(original, upper) {
    const words = splitIntoWords(original);
    return combineWords(words, legalizeName, upper ? firstUpperWordStyle : allLowerWordStyle, firstUpperWordStyle, upper ? allUpperWordStyle : allLowerWordStyle, allUpperWordStyle, "", isLetterOrUnderscore);
}
export const upperNamingFunction = funPrefixNamer("upper", (n) => elmNameStyle(n, true));
export const lowerNamingFunction = funPrefixNamer("lower", (n) => elmNameStyle(n, false));
export function requiredOrOptional(p) {
    function optional(fallback) {
        return { reqOrOpt: "Jpipe.optional", fallback };
    }
    const t = p.type;
    if (p.isOptional ||
        (t instanceof UnionType && nullableFromUnion(t) !== null)) {
        return optional(" Nothing");
    }
    if (t.kind === "null") {
        return optional(" ()");
    }
    return { reqOrOpt: "Jpipe.required", fallback: "" };
}
