import {PersonaPrompt} from "../../datatypes/Persona"; import {guidedEmpathyToStringShort, GuidedEmpathy} from "../../datatypes/GuidedEmpathy"; import {InquireFunction} from "../Inquire"; import {EntryMainMenuChoice, makeEntryMainMenuChoice} from "./EntryMainMenuPrompt"; import chalk from "chalk"; import pluralize from "pluralize"; import {isPopulatedArray} from "../../utils/Arrays"; import {ListChoiceOptions} from "inquirer"; import {asDefault} from "../../utils/Objects"; import {GuidedEmpathyPromptOptions} from "./GuidedEmpathyPrompt"; export interface GuidedEmpathyListPromptOptions extends PersonaPrompt { readonly default?: readonly GuidedEmpathy[] } export interface GuidedEmpathyListPromptDependencies { readonly inquire: InquireFunction readonly promptForEmpathy: (input: GuidedEmpathyPromptOptions) => Promise } export function guidedEmpathyListPrompt(deps: GuidedEmpathyListPromptDependencies): { (options: GuidedEmpathyListPromptOptions): Promise; mainMenu: EntryMainMenuChoice<"guidedEmpathy"> } { return makeEntryMainMenuChoice({ property: "guidedEmpathy", name: (input) => typeof input === "object" ? `Continue guided empathy ${chalk.dim(`(currently ${chalk.greenBright(`${pluralize("entry", input.length, true)}`)})`)}` : "Do some guided empathy", key: "e", injected: (options) => promptForGuidedEmpathyList(options, deps), toOptions: asDefault, toProperty: (input) => input.length === 0 ? undefined : input, }) } export async function promptForGuidedEmpathyList(options: GuidedEmpathyListPromptOptions, deps: GuidedEmpathyListPromptDependencies): Promise { const {inquire, promptForEmpathy} = deps if (!isPopulatedArray(options.default)) { const empathy = await promptForEmpathy(typeof options.persona === "string" ? {persona: options.persona} : {}) if (empathy === null) { return [] } else { return promptForGuidedEmpathyList({ ...options, default: [empathy], }, deps) } } else { const result = await inquire({ type: "list", message: "Want to change an existing entry or add a new one?", default: options.default.length, choices: [ ...options.default.map((item, index): ListChoiceOptions => ({ name: guidedEmpathyToStringShort(item), value: index, short: guidedEmpathyToStringShort(item) })), { name: "Add New Entry", value: -1, short: "Add New Entry" }, { name: "Done", value: -2, short: "Done" } ] }) if (result === -1) { const empathy = await promptForEmpathy(typeof options.persona === "string" ? {persona: options.persona} : {}) if (empathy === null) { if (!isPopulatedArray(options.default)) { return [] } else { return promptForGuidedEmpathyList({ ...options, default: options.default, }, deps) } } else { return promptForGuidedEmpathyList({ ...options, default: [...options.default, empathy], }, deps) } } else if (result === -2) { return options.default } else { const empathy = await promptForEmpathy(typeof options.persona === "string" ? {persona: options.persona, default: options.default[result]} : {default: options.default[result]}) if (empathy === null) { if (options.default.length === 1) { return [] } else { return promptForGuidedEmpathyList({ ...options, default: [...options.default.slice(0, result), ...options.default.slice(result + 1)] }, deps) } } else { return promptForGuidedEmpathyList({ ...options, default: [...options.default.slice(0, result), empathy, ...options.default.slice(result + 1)] }, deps) } } } }