<commit_before>import { Relationship } from '@keystonejs/fields';

import type { FieldConfig } from '../../interfaces';
import type { FieldType } from '@keystone-next/types';
import type { BaseGeneratedListTypes, FieldDefaultValue } from '@keystone-next/types';
import { resolveView } from '../../resolve-view';

// This is the default display mode for Relationships
type SelectDisplayConfig = {
  ui?: {
    // Sets the relationship to display as a Select field
    displayMode?: 'select';
    /**
     * The path of the field to use from the related list for item labels in the select.
     * Defaults to the labelField configured on the related list.
     */
    labelField?: string;
  };
};

type CardsDisplayConfig = {
  ui?: {
    // Sets the relationship to display as a list of Cards
    displayMode: 'cards';
    /* The set of fields to render in the default Card component **/
    cardFields: string[];
    /** Causes the default Card component to render as a link to navigate to the related item */
    linkToItem?: boolean;
    /** Determines whether removing a related item in the UI will delete or unlink it */
    removeMode?: 'disconnect' | 'none'; // | 'delete';
    /** Configures inline create mode for cards (alternative to opening the create modal) */
    inlineCreate?: { fields: string[] };
    /** Configures inline edit mode for cards */
    inlineEdit?: { fields: string[] };
    /** Configures whether a select to add existing items should be shown or not */
    inlineConnect?: boolean;
  };
};

export type RelationshipFieldConfig<
  TGeneratedListTypes extends BaseGeneratedListTypes
> = FieldConfig<TGeneratedListTypes> & {
  many?: boolean;
  ref: string;
  ui?: {
    hideCreate?: boolean;
  };
  defaultValue?: FieldDefaultValue<Record<string, unknown>>;
  isIndexed?: boolean;
  isUnique?: boolean;
} & (SelectDisplayConfig | CardsDisplayConfig);

const views = resolveView('relationship/views');

export const relationship = <TGeneratedListTypes extends BaseGeneratedListTypes>(
  config: RelationshipFieldConfig<TGeneratedListTypes>
): FieldType<TGeneratedListTypes> => ({
  type: Relationship,
  config,
  views,
  getAdminMeta(
    listKey,
    path,
    adminMeta
  ): Parameters<
    typeof import('@keystone-next/fields/types/relationship/views').controller
  >[0]['fieldMeta'] {
    const refListKey = config.ref.split('.')[0];
    return {
      refListKey,
      many: config.many ?? false,
      hideCreate: config.ui?.hideCreate ?? false,
      ...(config.ui?.displayMode === 'cards'
        ? {
            displayMode: 'cards',
            cardFields: config.ui.cardFields,
            linkToItem: config.ui.linkToItem ?? false,
            removeMode: config.ui.removeMode ?? 'disconnect',
            inlineCreate: config.ui.inlineCreate ?? null,
            inlineEdit: config.ui.inlineEdit ?? null,
            inlineConnect: config.ui.inlineConnect ?? false,
          }
        : {
            displayMode: 'select',
            refLabelField: adminMeta.lists[refListKey].labelField,
          }),<commit_msg>Improve error message when relationships have an invalid ref listKey (#4211)<commit_after>import { Relationship } from '@keystonejs/fields';

import type { FieldConfig } from '../../interfaces';
import type { FieldType } from '@keystone-next/types';
import type { BaseGeneratedListTypes, FieldDefaultValue } from '@keystone-next/types';
import { resolveView } from '../../resolve-view';

// This is the default display mode for Relationships
type SelectDisplayConfig = {
  ui?: {
    // Sets the relationship to display as a Select field
    displayMode?: 'select';
    /**
     * The path of the field to use from the related list for item labels in the select.
     * Defaults to the labelField configured on the related list.
     */
    labelField?: string;
  };
};

type CardsDisplayConfig = {
  ui?: {
    // Sets the relationship to display as a list of Cards
    displayMode: 'cards';
    /* The set of fields to render in the default Card component **/
    cardFields: string[];
    /** Causes the default Card component to render as a link to navigate to the related item */
    linkToItem?: boolean;
    /** Determines whether removing a related item in the UI will delete or unlink it */
    removeMode?: 'disconnect' | 'none'; // | 'delete';
    /** Configures inline create mode for cards (alternative to opening the create modal) */
    inlineCreate?: { fields: string[] };
    /** Configures inline edit mode for cards */
    inlineEdit?: { fields: string[] };
    /** Configures whether a select to add existing items should be shown or not */
    inlineConnect?: boolean;
  };
};

export type RelationshipFieldConfig<
  TGeneratedListTypes extends BaseGeneratedListTypes
> = FieldConfig<TGeneratedListTypes> & {
  many?: boolean;
  ref: string;
  ui?: {
    hideCreate?: boolean;
  };
  defaultValue?: FieldDefaultValue<Record<string, unknown>>;
  isIndexed?: boolean;
  isUnique?: boolean;
} & (SelectDisplayConfig | CardsDisplayConfig);

const views = resolveView('relationship/views');

export const relationship = <TGeneratedListTypes extends BaseGeneratedListTypes>(
  config: RelationshipFieldConfig<TGeneratedListTypes>
): FieldType<TGeneratedListTypes> => ({
  type: Relationship,
  config,
  views,
  getAdminMeta(
    listKey,
    path,
    adminMeta
  ): Parameters<
    typeof import('@keystone-next/fields/types/relationship/views').controller
  >[0]['fieldMeta'] {
    const refListKey = config.ref.split('.')[0];
    if (!adminMeta.lists[refListKey]) {
      throw new Error(`The ref [${config.ref}] on relationship [${listKey}.${path}] is invalid`);
    }
    return {
      refListKey,
      many: config.many ?? false,
      hideCreate: config.ui?.hideCreate ?? false,
      ...(config.ui?.displayMode === 'cards'
        ? {
            displayMode: 'cards',
            cardFields: config.ui.cardFields,
            linkToItem: config.ui.linkToItem ?? false,
            removeMode: config.ui.removeMode ?? 'disconnect',
            inlineCreate: config.ui.inlineCreate ?? null,
            inlineEdit: config.ui.inlineEdit ?? null,
            inlineConnect: config.ui.inlineConnect ?? false,
          }
        : {
            displayMode: 'select',
            refLabelField: adminMeta.lists[refListKey].labelField,
          }),
{'fraction_non_alphanumeric': 0.11633780584056827, 'fraction_numerical': 0.0012628255722178373, 'mean_word_length': 3.1739130434782608, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3516564', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1685, 'n_words': 542}