{"version":3,"file":"dynamic-entities.cjs","names":[],"sources":["../src/dynamic-entities.ts"],"sourcesContent":["import type { ThunkArgs } from '@wordpress/data';\nimport type { GetRecordsHttpQuery, State } from './selectors';\nimport type * as ET from './entity-types';\n\nexport type WPEntityTypes< C extends ET.Context = 'edit' > = {\n\tComment: ET.Comment< C >;\n\tGlobalStyles: ET.GlobalStyles< C >;\n\tMedia: ET.Attachment< C >;\n\tMenu: ET.NavMenu< C >;\n\tMenuItem: ET.NavMenuItem< C >;\n\tMenuLocation: ET.MenuLocation< C >;\n\tPlugin: ET.Plugin< C >;\n\tPostType: ET.Type< C >;\n\tRevision: ET.PostRevision< C >;\n\tSidebar: ET.Sidebar< C >;\n\tSite: ET.Settings< C >;\n\tStatus: ET.PostStatusObject< C >;\n\tTaxonomy: ET.Taxonomy< C >;\n\tTerm: ET.Term< C >;\n\tTheme: ET.Theme< C >;\n\tUnstableBase: ET.UnstableBase< C >;\n\tUser: ET.User< C >;\n\tWidget: ET.Widget< C >;\n\tWidgetType: ET.WidgetType< C >;\n};\n\n/**\n * A simple utility that pluralizes a string.\n * Converts:\n * - \"post\" to \"posts\"\n * - \"taxonomy\" to \"taxonomies\"\n * - \"media\" to \"mediaItems\"\n * - \"status\" to \"statuses\"\n *\n * It does not pluralize \"GlobalStyles\" due to lack of clarity about it at time of writing.\n */\ntype PluralizeEntity< T extends string > = T extends 'GlobalStyles'\n\t? never\n\t: T extends 'Media'\n\t\t? 'MediaItems'\n\t\t: T extends 'Status'\n\t\t\t? 'Statuses'\n\t\t\t: T extends `${ infer U }y`\n\t\t\t\t? `${ U }ies`\n\t\t\t\t: `${ T }s`;\n\n/**\n * A simple utility that singularizes a string.\n *\n * Converts:\n * - \"posts\" to \"post\"\n * - \"taxonomies\" to \"taxonomy\"\n * - \"mediaItems\" to \"media\"\n * - \"statuses\" to \"status\"\n */\ntype SingularizeEntity< T extends string > = T extends 'MediaItems'\n\t? 'Media'\n\t: T extends 'Statuses'\n\t\t? 'Status'\n\t\t: T extends `${ infer U }ies`\n\t\t\t? `${ U }y`\n\t\t\t: T extends `${ infer U }s`\n\t\t\t\t? U\n\t\t\t\t: T;\n\nexport type SingularGetters = {\n\t[ Key in `get${ keyof WPEntityTypes }` ]: (\n\t\tstate: State,\n\t\tid: number | string,\n\t\tquery?: GetRecordsHttpQuery\n\t) => WPEntityTypes[ Key extends `get${ infer E }` ? E : never ] | undefined;\n};\n\nexport type PluralGetters = {\n\t[ Key in `get${ PluralizeEntity< keyof WPEntityTypes > }` ]: (\n\t\tstate: State,\n\t\tquery?: GetRecordsHttpQuery\n\t) => Array<\n\t\tWPEntityTypes[ Key extends `get${ infer E }`\n\t\t\t? SingularizeEntity< E >\n\t\t\t: never ]\n\t> | null;\n};\n\ntype ActionOptions = {\n\tthrowOnError?: boolean;\n};\n\ntype DeleteRecordsHttpQuery = Record< string, any >;\n\n/**\n * Typed as thunks, like the runtime wrappers in `index.js`, so dispatching\n * resolves to a single Promise instead of `Promise< Promise< ... > >`.\n */\nexport type SaveActions = {\n\t[ Key in `save${ keyof WPEntityTypes }` ]: (\n\t\tdata: Key extends 'saveGlobalStyles'\n\t\t\t? ET.GlobalStylesUpdate\n\t\t\t: Partial<\n\t\t\t\t\tWPEntityTypes[ Key extends `save${ infer E }` ? E : never ]\n\t\t\t\t>,\n\t\toptions?: ActionOptions\n\t) => (\n\t\tthunkArgs: ThunkArgs\n\t) => Promise<\n\t\tWPEntityTypes[ Key extends `save${ infer E }` ? E : never ] | undefined\n\t>;\n};\n\nexport type DeleteActions = {\n\t[ Key in `delete${ keyof WPEntityTypes }` ]: (\n\t\tid: number | string,\n\t\tquery?: DeleteRecordsHttpQuery,\n\t\toptions?: ActionOptions\n\t) => ( thunkArgs: ThunkArgs ) => Promise<\n\t\t| WPEntityTypes[ Key extends `delete${ infer E }` ? E : never ]\n\t\t| {\n\t\t\t\tdeleted: true;\n\t\t\t\tprevious: WPEntityTypes[ Key extends `delete${ infer E }`\n\t\t\t\t\t? E\n\t\t\t\t\t: never ];\n\t\t  }\n\t\t| false\n\t\t| undefined\n\t>;\n};\n\nexport let dynamicActions: SaveActions & DeleteActions;\n\nexport let dynamicSelectors: SingularGetters & PluralGetters;\n"],"mappings":";;;;;;AA+HA,IAAW;AAEX,IAAW"}