"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDisplayName = exports.parseTypeNames = exports.capitalize = exports.getPluralName = exports.generatePlural = void 0;
const plur_1 = __importDefault(require("plur"));
// /**
//  * Call function once.
//  */
// export const once = <A extends any[], R, T>(
//   fn: (this: T, ...arg: A) => R
// ): ((this: T, ...arg: A) => R | undefined) => {
//   let done = false
//   return function (this: T, ...args: A) {
//     return done ? void 0 : ((done = true), fn.apply(this, args))
//   }
// }
const generatePlural = (string) => {
    return (0, plur_1.default)(string) || `${string}s`;
};
exports.generatePlural = generatePlural;
const getPluralName = (schema, type) => {
    let pluralName = schema.types[type]?.meta?.pluralName;
    if (!pluralName) {
        const name = schema.types[type]?.meta?.name || type;
        pluralName = (0, exports.generatePlural)(name);
    }
    return (0, exports.capitalize)(pluralName);
};
exports.getPluralName = getPluralName;
// export const getName = (schema: any, type: string) => {
//   return capitalize(schema.types[type]?.meta?.name || type)
// }
const capitalize = (name, allWords) => {
    if (!name) {
        return '';
    }
    if (allWords) {
        return name
            .split(' ')
            .map((word) => (0, exports.capitalize)(word))
            .join(' ');
    }
    return name[0].toUpperCase() + name.substring(1);
};
exports.capitalize = capitalize;
const formatter = new Intl.ListFormat('en', {
    style: 'long',
    type: 'disjunction',
});
const parseTypeNames = (schema, field, max = 5) => {
    let types = formatter.format(field.meta?.refTypes?.slice(0, max).map((t) => {
        return (0, exports.getPluralName)(schema, t);
    })) || 'references';
    if (field.meta?.refTypes?.length > max) {
        types += ' (and ' + (field.meta?.refTypes?.length - max) + ' more)';
    }
    return types;
};
exports.parseTypeNames = parseTypeNames;
const parseDisplayName = (field, name) => {
    let displayName = field.meta?.name || name;
    if (displayName === 'createdAt') {
        displayName = 'Created At';
    }
    if (displayName === 'updatedAt') {
        displayName = 'Updated At';
    }
    displayName = (0, exports.capitalize)(displayName);
    return displayName;
};
exports.parseDisplayName = parseDisplayName;
//# sourceMappingURL=generatePlurals.js.map