/** @category Schema Construction */

import { naturalCompare } from '../jsutils/naturalCompare.ts';
import type { ObjMap } from '../jsutils/ObjMap.ts';

import { GraphQLSchema } from '../type/schema.ts';

import { mapSchemaConfig, SchemaElementKind } from './mapSchemaConfig.ts';

/**
 * Sort GraphQLSchema.
 *
 * This function returns a sorted copy of the given GraphQLSchema.
 * @param schema - GraphQL schema to use.
 * @returns A copy of the schema with types, fields, arguments, and values sorted lexicographically.
 * @example
 * ```ts
 * import {
 *   buildSchema,
 *   lexicographicSortSchema,
 *   printSchema,
 * } from 'graphql/utilities';
 *
 * const schema = buildSchema(`
 *   type Query {
 *     zebra: String
 *     apple: String
 *   }
 *
 *   enum Episode {
 *     JEDI
 *     NEW_HOPE
 *     EMPIRE
 *   }
 * `);
 *
 * const sortedSchema = lexicographicSortSchema(schema);
 *
 * printSchema(sortedSchema);
 * // =>
 * // enum Episode {
 * //   EMPIRE
 * //   JEDI
 * //   NEW_HOPE
 * // }
 * //
 * // type Query {
 * //   apple: String
 * //   zebra: String
 * // }
 * ```
 */
export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
  return new GraphQLSchema(
    mapSchemaConfig(schema.toConfig(), () => ({
      [SchemaElementKind.OBJECT]: (config) => ({
        ...config,
        interfaces: () => sortByName(config.interfaces()),
        fields: () => sortObjMap(config.fields()),
      }),
      [SchemaElementKind.FIELD]: (config) => ({
        ...config,
        args: sortObjMap(config.args),
      }),
      [SchemaElementKind.INTERFACE]: (config) => ({
        ...config,
        interfaces: () => sortByName(config.interfaces()),
        fields: () => sortObjMap(config.fields()),
      }),
      [SchemaElementKind.UNION]: (config) => ({
        ...config,
        types: () => sortByName(config.types()),
      }),
      [SchemaElementKind.ENUM]: (config) => ({
        ...config,
        values: () => sortObjMap(config.values()),
      }),
      [SchemaElementKind.INPUT_OBJECT]: (config) => ({
        ...config,
        fields: () => sortObjMap(config.fields()),
      }),
      [SchemaElementKind.DIRECTIVE]: (config) => ({
        ...config,
        locations: sortBy(config.locations, (x) => x),
        args: sortObjMap(config.args),
      }),
      [SchemaElementKind.SCHEMA]: (config) => ({
        ...config,
        types: sortByName(config.types),
        directives: sortByName(config.directives),
      }),
    })),
  );
}

function sortObjMap<T, R>(map: ObjMap<T>): ObjMap<R> {
  const sortedMap = Object.create(null);
  for (const key of Object.keys(map).sort(naturalCompare)) {
    sortedMap[key] = map[key];
  }
  return sortedMap;
}

function sortByName<T extends { readonly name: string }>(
  array: ReadonlyArray<T>,
): Array<T> {
  return sortBy(array, (obj) => obj.name);
}

function sortBy<T>(
  array: ReadonlyArray<T>,
  mapToKey: (item: T) => string,
): Array<T> {
  return array.slice().sort((obj1, obj2) => {
    const key1 = mapToKey(obj1);
    const key2 = mapToKey(obj2);
    return naturalCompare(key1, key2);
  });
}