import { compare } from './compare';
import { getImportExportKind } from './getImportExportKind';

export function sortSpecifierItems(items: any[]): string[] {
  return items.slice().sort(
    (itemA, itemB) =>
      // Put type imports/exports before regular ones.
      compare(getImportExportKind(itemA.node), getImportExportKind(itemB.node)) ||
      // Then compare by imported or exported name (external interface name).
      // import { a as b } from "a"
      //          ^
      // export { b as a }
      //               ^
      compare((itemA.node.imported || itemA.node.exported).name, (itemB.node.imported || itemB.node.exported).name) ||
      // Then compare by the file-local name.
      // import { a as b } from "a"
      //               ^
      // export { b as a }
      //          ^
      compare(itemA.node.local.name, itemB.node.local.name) ||
      // Keep the original order if the names are the same. It’s not worth
      // trying to compare anything else, `import {a, a} from "mod"` is a syntax
      // error anyway (but @babel/eslint-parser kind of supports it).
      // istanbul ignore next
      itemA.index - itemB.index
  );
}
