/* eslint-disable @typescript-eslint/no-explicit-any */
declare module 'decap-cms-core' {
  import type { ComponentType } from 'react';
  import type { List, Map } from 'immutable';
  import type { Pluggable } from 'unified';

  export type CmsBackendType =
    | 'azure'
    | 'git-gateway'
    | 'github'
    | 'turbo-github'
    | 'turbo-gitlab'
    | 'gitlab'
    | 'gitea'
    | 'forgejo'
    | 'bitbucket'
    | 'test-repo'
    | 'proxy';

  export type CmsMapWidgetType = 'Point' | 'LineString' | 'Polygon';

  export type CmsMarkdownWidgetButton =
    | 'bold'
    | 'italic'
    | 'code'
    | 'link'
    | 'heading-one'
    | 'heading-two'
    | 'heading-three'
    | 'heading-four'
    | 'heading-five'
    | 'heading-six'
    | 'quote'
    | 'code-block'
    | 'bulleted-list'
    | 'numbered-list';

  export interface CmsSelectWidgetOptionObject {
    label: string;
    value: any;
  }

  export type CmsCollectionFormatType = string;

  export type CmsAuthScope = 'repo' | 'public_repo';

  export type CmsPublishMode = 'simple' | 'editorial_workflow' | '';

  export type CmsSlugEncoding = 'unicode' | 'ascii';

  export interface CmsI18nConfig {
    structure: 'multiple_folders' | 'multiple_files' | 'single_file';
    locales: string[];
    default_locale?: string;
  }

  export interface CmsFieldBase {
    name: string;
    label?: string;
    required?: boolean;
    hint?: string;
    pattern?: [string | RegExp, string];
    i18n?: boolean | 'translate' | 'duplicate' | 'none';
    media_folder?: string;
    public_folder?: string;
    media_processing?: CmsMediaProcessing;
    comment?: string;
  }

  export interface CmsFieldBoolean {
    widget: 'boolean';
    default?: boolean;
  }

  export interface CmsFieldCode {
    widget: 'code';
    default?: any;

    default_language?: string;
    allow_language_selection?: boolean;
    keys?: { code: string; lang: string };
    output_code_only?: boolean;
  }

  export interface CmsFieldColor {
    widget: 'color';
    default?: string;

    allowInput?: boolean;
    enableAlpha?: boolean;
  }

  export interface CmsFieldDateTime {
    widget: 'datetime';
    default?: string;

    format?: string;
    date_format?: boolean | string;
    time_format?: boolean | string;
    picker_utc?: boolean;

    /**
     * @deprecated Use date_format instead
     */
    dateFormat?: boolean | string;
    /**
     * @deprecated Use time_format instead
     */
    timeFormat?: boolean | string;
    /**
     * @deprecated Use picker_utc instead
     */
    pickerUtc?: boolean;
  }

  export interface CmsFieldFileOrImage {
    widget: 'file' | 'image';
    default?: string;

    media_library?: CmsMediaLibrary;
    allow_multiple?: boolean;
    choose_url?: boolean;
    config?: any;
  }

  export interface CmsFieldObject {
    widget: 'object';
    default?: any;

    collapsed?: boolean;
    summary?: string;
    fields: CmsField[];
  }

  export interface CmsFieldList {
    widget: 'list';
    default?: any;

    allow_add?: boolean;
    allow_remove?: boolean;
    allow_reorder?: boolean;
    collapsed?: boolean;
    summary?: string;
    minimize_collapsed?: boolean;
    label_singular?: string;
    field?: CmsField;
    fields?: CmsField[];
    max?: number;
    min?: number;
    add_to_top?: boolean;
    types?: (CmsFieldBase & CmsFieldObject)[];
  }

  export interface CmsFieldMap {
    widget: 'map';
    default?: string;

    decimals?: number;
    type?: CmsMapWidgetType;
  }

  export interface CmsFieldMarkdown {
    widget: 'markdown';
    default?: string;

    minimal?: boolean;
    buttons?: CmsMarkdownWidgetButton[];
    editor_components?: string[];
    modes?: ('raw' | 'rich_text')[];

    /**
     * @deprecated Use editor_components instead
     */
    editorComponents?: string[];
  }

  export interface CmsFieldRichText {
    widget: 'richtext';
    default?: string;

    minimal?: boolean;
    buttons?: CmsMarkdownWidgetButton[];
    editor_components?: string[];
    modes?: ('raw' | 'rich_text')[];

    /**
     * @deprecated Use editor_components instead
     */
    editorComponents?: string[];
  }

  export interface CmsFieldNumber {
    widget: 'number';
    default?: string | number;

    value_type?: 'int' | 'float' | string;
    min?: number;
    max?: number;

    step?: number;

    /**
     * @deprecated Use valueType instead
     */
    valueType?: 'int' | 'float' | string;
  }

  export interface CmsFieldSelect {
    widget: 'select';
    default?: string | string[];

    options: string[] | CmsSelectWidgetOptionObject[];
    multiple?: boolean;
    min?: number;
    max?: number;
  }

  export interface CmsFieldRelation {
    widget: 'relation';
    default?: string | string[];

    collection: string;
    value_field: string;
    search_fields: string[];
    file?: string;
    display_fields?: string[];
    multiple?: boolean;
    min?: number;
    max?: number;
    options_length?: number;

    /**
     * @deprecated Use value_field instead
     */
    valueField?: string;
    /**
     * @deprecated Use search_fields instead
     */
    searchFields?: string[];
    /**
     * @deprecated Use display_fields instead
     */
    displayFields?: string[];
    /**
     * @deprecated Use options_length instead
     */
    optionsLength?: number;
  }

  export interface CmsFieldHidden {
    widget: 'hidden';
    default?: any;
  }

  export interface CmsFieldStringOrText {
    // This is the default widget, so declaring its type is optional.
    widget?: 'string' | 'text';
    default?: string;
    visualEditing?: boolean;
  }

  export interface CmsFieldMeta {
    name: string;
    label: string;
    widget: string;
    required: boolean;
    index_file: string;
    meta: boolean;
  }

  export type CmsField = CmsFieldBase &
    (
      | CmsFieldBoolean
      | CmsFieldCode
      | CmsFieldColor
      | CmsFieldDateTime
      | CmsFieldFileOrImage
      | CmsFieldList
      | CmsFieldMap
      | CmsFieldMarkdown
      | CmsFieldRichText
      | CmsFieldNumber
      | CmsFieldObject
      | CmsFieldRelation
      | CmsFieldSelect
      | CmsFieldHidden
      | CmsFieldStringOrText
      | CmsFieldMeta
    );

  export interface CmsCollectionFile {
    name: string;
    label: string;
    file: string;
    fields: CmsField[];
    label_singular?: string;
    description?: string;
    preview_path?: string;
    preview_path_date_field?: string;
    i18n?: boolean | CmsI18nConfig;
    media_folder?: string;
    public_folder?: string;
  }

  export interface ViewFilter {
    label: string;
    field: string;
    pattern: string | boolean;
  }

  export interface ViewGroup {
    label: string;
    field: string;
    pattern?: string;
  }

  export interface SortableField {
    field: string;
    label?: string;
    default_sort?: boolean | 'asc' | 'desc';
  }

  export interface CmsCollection {
    name: string;
    label: string;
    label_singular?: string;
    description?: string;
    folder?: string;
    files?: CmsCollectionFile[];
    identifier_field?: string;
    summary?: string;
    slug?: string;
    preview_path?: string;
    preview_path_date_field?: string;
    preview_path_preserve_slashes?: boolean;
    create?: boolean;
    delete?: boolean;
    hide?: boolean;
    editor?: {
      preview?: boolean;
      visualEditing?: boolean;
    };
    publish?: boolean;
    nested?: {
      depth: number;
      subfolders?: boolean;
    };
    meta?: { path?: { label: string; widget: string; index_file?: string } };

    /**
     * It accepts the following values: yml, yaml, toml, json, md, markdown, html
     *
     * You may also specify a custom extension not included in the list above, by specifying the format value.
     */
    extension?: string;
    format?: CmsCollectionFormatType;

    frontmatter_delimiter?: string[] | string;
    fields?: CmsField[];
    filter?: { field: string; value: any };
    path?: string;
    media_folder?: string;
    public_folder?: string;
    sortable_fields?: (string | SortableField)[];
    view_filters?: ViewFilter[];
    view_groups?: ViewGroup[];
    i18n?: boolean | CmsI18nConfig;
    limit?: number;

    /**
     * @deprecated Use sortable_fields instead
     */
    sortableFields?: (string | SortableField)[];
  }

  export interface CmsBackend {
    name: CmsBackendType;
    auth_scope?: CmsAuthScope;
    open_authoring?: boolean;
    always_fork?: boolean;
    repo?: string;
    branch?: string;
    api_root?: string;
    site_domain?: string;
    base_url?: string;
    auth_endpoint?: string;
    app_id?: string;
    turbo_site_id?: string;
    turbo_config_url?: string;
    supabase_app_id?: string;
    supabase_anon_key?: string;
    /**
     * Decap Turbo: set true to turn on the editor bridge (AI agents setting fields in the open entry). Off by default.
     * @experimental Still in development; may change or go away in any release.
     */
    editor_bridge?: boolean;
    auth_type?: 'implicit' | 'pkce';
    cms_label_prefix?: string;
    squash_merges?: boolean;
    signoff_commits?: boolean;
    proxy_url?: string;
    commit_messages?: {
      create?: string;
      update?: string;
      delete?: string;
      uploadMedia?: string;
      deleteMedia?: string;
      openAuthoring?: string;
    };
  }

  export interface CmsSlug {
    encoding?: CmsSlugEncoding;
    clean_accents?: boolean;
    sanitize_replacement?: string;
  }

  export interface CmsLocalBackend {
    url?: string;
    allowed_hosts?: string[];
  }

  export interface CmsIssueReports {
    url?: string;
  }

  export type CmsMediaProcessingFormat = 'jpeg' | 'webp';

  export interface CmsMediaProcessing {
    enabled: boolean;
    format?: {
      enabled: boolean;
      default: CmsMediaProcessingFormat;
    };
    quality?: number;
    strip_metadata?: boolean;
    width?: number | null;
    height?: number | null;
    aspect_ratio?: number | string | null;
  }

  export interface CmsConfig {
    backend: CmsBackend;
    collections: CmsCollection[];
    locale?: string;
    site_url?: string;
    display_url?: string;
    logo_url?: string; // Deprecated, replaced by `logo.src`
    logo?: {
      src: string;
      show_in_header?: boolean;
    };
    show_preview_links?: boolean;
    media_folder?: string;
    public_folder?: string;
    media_folder_relative?: boolean;
    media_processing?: CmsMediaProcessing;
    media_library?: CmsMediaLibrary;
    publish_mode?: CmsPublishMode;
    issue_reports?: CmsIssueReports;
    load_config_file?: boolean;
    integrations?: {
      hooks: string[];
      provider: string;
      collections?: '*' | string[];
      applicationID?: string;
      apiKey?: string;
      getSignedFormURL?: string;
    }[];
    slug?: CmsSlug;
    i18n?: CmsI18nConfig;
    local_backend?: boolean | CmsLocalBackend;
    editor?: {
      notes?: boolean;
      preview?: boolean;
    };
  }

  export interface InitOptions {
    config: CmsConfig;
  }

  export type EditorComponentField =
    | ({
        name: string;
        label: string;
      } & {
        widget: Omit<string, 'list'>;
      })
    | {
        widget: 'list';
        /**
         * Used if widget === "list" to create a flat array
         */
        field?: EditorComponentField;
        /**
         * Used if widget === "list" to create an array of objects
         */
        fields?: EditorComponentField[];
      };

  export interface BlockEditorComponentOptions {
    id: string;
    label: string;
    type?: 'block';
    fields?: EditorComponentField[];
    pattern: RegExp;
    allow_add?: boolean;
    fromBlock: (match: RegExpMatchArray) => any;
    toBlock: (data: any) => string;
    toPreview: (data: any) => string | JSX.Element;
  }

  export interface InlineEditorComponentOptions {
    id: string;
    label: string;
    type: 'inline';
    trigger?: string;
    pattern: RegExp;
    fromInline: (match: RegExpExecArray) => Record<string, any>;
    toInline: (data: Record<string, any>) => string;
    toPreview: (data: Record<string, any>) => React.ReactNode;
    onInsert?: (context: {
      selectedText: string;
      cmsContext: any;
    }) => Promise<Record<string, any> | null>;
    onEdit?: (context: { data: Record<string, any> }) => Promise<Record<string, any> | null>;
  }

  export type EditorComponentOptions = BlockEditorComponentOptions | InlineEditorComponentOptions;

  export interface PreviewStyleOptions {
    raw: boolean;
  }

  export interface PreviewStyle extends PreviewStyleOptions {
    value: string;
  }

  export type CmsBackendClass = any; // TODO: type properly

  export interface CmsRegistryBackend {
    init: (args: any) => CmsBackendClass;
  }

  export interface CmsWidgetControlProps<T = any> {
    value: T;
    field: Map<string, any>;
    onChange: (value: T) => void;
    forID: string;
    classNameWrapper: string;
  }

  export interface CmsWidgetPreviewProps<T = any> {
    value: T;
    field: Map<string, any>;
    metadata: Map<string, any>;
    getAsset: GetAssetFunction;
    entry: Map<string, any>;
    fieldsMetaData: Map<string, any>;
  }

  export interface CmsWidgetParam<T = any> {
    name: string;
    controlComponent: CmsWidgetControlProps<T>;
    previewComponent?: CmsWidgetPreviewProps<T>;
    globalStyles?: any;
  }

  export interface CmsWidget<T = any> {
    control: CmsWidgetControlProps<T>;
    preview?: CmsWidgetPreviewProps<T>;
    globalStyles?: any;
  }

  export type CmsWidgetValueSerializer = any; // TODO: type properly

  export type CmsMediaLibraryOptions = any; // TODO: type properly

  export interface CmsMediaLibrary {
    name: string;
    config?: CmsMediaLibraryOptions;
  }

  export interface CmsEventListener {
    name: 'prePublish' | 'postPublish' | 'preUnpublish' | 'postUnpublish' | 'preSave' | 'postSave';
    handler: ({
      entry,
      author,
    }: {
      entry: Map<string, any>;
      author: { login: string; name: string };
    }) => any;
  }

  export type CmsEventListenerOptions = any; // TODO: type properly

  export type CmsLocalePhrases = any; // TODO: type properly

  export type Formatter = {
    fromFile(content: string): unknown;
    toFile(data: object, sortedKeys?: string[], comments?: Record<string, string>): string;
  };

  export interface CmsRegistry {
    backends: {
      [name: string]: CmsRegistryBackend;
    };
    templates: {
      [name: string]: ComponentType<any>;
    };
    previewStyles: PreviewStyle[];
    widgets: {
      [name: string]: CmsWidget;
    };
    editorComponents: Map<string, ComponentType<any>>;
    widgetValueSerializers: {
      [name: string]: CmsWidgetValueSerializer;
    };
    mediaLibraries: CmsMediaLibrary[];
    locales: {
      [name: string]: CmsLocalePhrases;
    };
    formats: {
      [name: string]: Formatter;
    };
  }

  type GetAssetFunction = (asset: string) => {
    url: string;
    path: string;
    field?: any;
    fileObj: File;
  };

  export type PreviewTemplateComponentProps = {
    entry: Map<string, any>;
    collection: Map<string, any>;
    getCollection: (collectionName: string, slug?: string) => Promise<Map<string, any>[]>;
    widgetFor: (name: any, fields?: any, values?: any, fieldsMetaData?: any) => JSX.Element | null;
    widgetsFor: (name: any) => any;
    getAsset: GetAssetFunction;
    boundGetAsset: (collection: any, path: any) => GetAssetFunction;
    fieldsMetaData: Map<string, any>;
    config: Map<string, any>;
    fields: List<Map<string, any>>;
    isLoadingAsset: boolean;
    window: Window;
    document: Document;
  };

  /**
   * The entry open in the editor, as `CMS.editor` reports it.
   * @experimental Not yet a stable public API; see `CMS.editor`.
   */
  export interface CmsEditorSnapshot {
    collection: string;
    collectionLabel: string;
    /** The entry's slug, or the file's name in a file collection; null for a new entry. */
    slug: string | null;
    newRecord: boolean;
    path: string | null;
    /** The collection's field definitions, as in config.yml. */
    fields: Record<string, unknown>[];
    /** The current, possibly unsaved, values of the default locale. */
    data: Record<string, unknown>;
    /** Other locales' values, keyed by locale, when the collection has i18n. */
    i18n: Record<string, { data: Record<string, unknown> }> | null;
    locales: string[] | null;
    defaultLocale: string | null;
    hasChanged: boolean;
  }

  /**
   * One change to the open entry: `path` is a field name, or a dotted path into
   * an object or list (`seo.description`, `tags.0`).
   * @experimental Not yet a stable public API; see `CMS.editor`.
   */
  export interface CmsFieldPatch {
    path: string;
    value: unknown;
  }

  /** @experimental Not yet a stable public API; see `CMS.editor`. */
  export interface CmsFieldPatchResult {
    applied: string[];
    rejected: { path: string; reason: string }[];
  }

  /**
   * The editor, reachable from outside React. Nothing here saves: patches
   * change the draft the way typing would, and the entry is written only when
   * someone presses Save.
   * @experimental Not yet a stable public API; see `CMS.editor`.
   */
  export interface CmsEditorApi {
    /** The open entry, or null when no entry is open. */
    getCurrentEntry: () => CmsEditorSnapshot | null;
    /** Changes fields of the open entry without saving; each patch is applied or rejected with a reason. */
    applyFieldPatch: (
      patches: CmsFieldPatch[],
      options?: { locale?: string },
    ) => CmsFieldPatchResult;
    /** Calls `listener` on every change to the open entry; returns the unsubscribe function. */
    onEditorChange: (listener: (snapshot: CmsEditorSnapshot | null) => void) => () => void;
  }

  /**
   * What a field action's `onClick` and `isAvailable` receive. Plain values,
   * not the editor's Immutable state.
   * @experimental Not yet a stable public API; see `CMS.registerFieldAction`.
   */
  export interface CmsFieldActionContext {
    /** The field's definition, as in config.yml. */
    field: Record<string, unknown>;
    /** The collection's name. */
    collection?: string;
    locale?: string;
    /** The open entry, read when accessed. */
    readonly entry: CmsEditorSnapshot | null;
    applyFieldPatch: CmsEditorApi['applyFieldPatch'];
  }

  /**
   * A button shown beside each top-level field's label in the editor.
   * @experimental Not yet a stable public API; see `CMS.registerFieldAction`.
   */
  export interface CmsFieldAction {
    /** Registering another action with the same id replaces it. */
    id: string;
    label: string;
    /** Tooltip; defaults to `label`. */
    title?: string;
    onClick: (context: CmsFieldActionContext) => void;
    /** Leave out to show the action on every field. */
    isAvailable?: (context: CmsFieldActionContext) => boolean;
  }

  export interface CMS {
    getBackend: (name: string) => CmsRegistryBackend | undefined;
    getEditorComponents: () => Map<string, ComponentType<any>>;
    getRemarkPlugins: () => Array<Pluggable>;
    getLocale: (locale: string) => CmsLocalePhrases | undefined;
    getMediaLibrary: (name: string) => CmsMediaLibrary | undefined;
    getPreviewStyles: () => PreviewStyle[];
    getPreviewTemplate: (name: string) => ComponentType<PreviewTemplateComponentProps> | undefined;
    getWidget: (name: string) => CmsWidget | undefined;
    getWidgetValueSerializer: (widgetName: string) => CmsWidgetValueSerializer | undefined;
    init: (options?: InitOptions) => void;
    registerBackend: (name: string, backendClass: CmsBackendClass) => void;
    registerEditorComponent: (options: EditorComponentOptions) => void;
    registerRemarkPlugin: (plugin: Pluggable) => void;
    registerEventListener: (
      eventListener: CmsEventListener,
      options?: CmsEventListenerOptions,
    ) => void;
    registerLocale: (locale: string, phrases: CmsLocalePhrases) => void;
    registerMediaLibrary: (mediaLibrary: CmsMediaLibrary, options?: CmsMediaLibraryOptions) => void;
    registerPreviewStyle: (filePath: string, options?: PreviewStyleOptions) => void;
    registerPreviewTemplate: (
      name: string,
      component: ComponentType<PreviewTemplateComponentProps>,
    ) => void;
    registerWidget: (
      widget: string | CmsWidgetParam,
      control?: ComponentType<CmsWidgetControlProps> | string,
      preview?: ComponentType<CmsWidgetPreviewProps>,
    ) => void;
    registerWidgetValueSerializer: (
      widgetName: string,
      serializer: CmsWidgetValueSerializer,
    ) => void;
    resolveWidget: (name: string) => CmsWidget | undefined;
    registerCustomFormat: (name: string, extension: string, formatter: Formatter) => void;
    /**
     * Adds a button beside each top-level field's label in the editor.
     * @experimental Not yet a stable public API: built for the Decap Turbo
     * editor bridge, and may change or be removed in any release, minor and
     * patch included.
     */
    registerFieldAction: (action: CmsFieldAction) => void;
    /**
     * Reads the entry open in the editor and changes its fields without
     * saving.
     * @experimental Not yet a stable public API: built for the Decap Turbo
     * editor bridge, and may change or be removed in any release, minor and
     * patch included.
     */
    editor: CmsEditorApi;
  }

  export const DecapCmsCore: CMS;

  export default DecapCmsCore;
}
