{"version":3,"file":"create-route-property-order.utils.cjs","names":[],"sources":["../../../../src/rules/create-route-property-order/create-route-property-order.utils.ts"],"sourcesContent":["export function sortDataByOrder<T, TKey extends keyof T>(\n  data: Array<T> | ReadonlyArray<T>,\n  orderRules: ReadonlyArray<\n    Readonly<[ReadonlyArray<T[TKey]>, ReadonlyArray<T[TKey]>]>\n  >,\n  key: TKey,\n): Array<T> | null {\n  const getSubsetIndex = (\n    item: T[TKey],\n    subsets: ReadonlyArray<ReadonlyArray<T[TKey]> | Array<T[TKey]>>,\n  ): number | null => {\n    for (let i = 0; i < subsets.length; i++) {\n      if (subsets[i]?.includes(item)) {\n        return i\n      }\n    }\n    return null\n  }\n\n  const orderSets = orderRules.reduce(\n    (sets, [A, B]) => [...sets, A, B],\n    [] as Array<ReadonlyArray<T[TKey]> | Array<T[TKey]>>,\n  )\n\n  const inOrderArray = data.filter(\n    (item) => getSubsetIndex(item[key], orderSets) !== null,\n  )\n\n  let wasResorted = false as boolean\n\n  // Sort by the relative order defined by the rules\n  const sortedArray = inOrderArray.sort((a, b) => {\n    const aKey = a[key],\n      bKey = b[key]\n    const aSubsetIndex = getSubsetIndex(aKey, orderSets)\n    const bSubsetIndex = getSubsetIndex(bKey, orderSets)\n\n    // If both items belong to different subsets, sort by their subset order\n    if (\n      aSubsetIndex !== null &&\n      bSubsetIndex !== null &&\n      aSubsetIndex !== bSubsetIndex\n    ) {\n      return aSubsetIndex - bSubsetIndex\n    }\n\n    // If both items belong to the same subset or neither is in the subset, keep their relative order\n    return 0\n  })\n\n  const inOrderIterator = sortedArray.values()\n  const result = data.map((item) => {\n    if (getSubsetIndex(item[key], orderSets) !== null) {\n      const sortedItem = inOrderIterator.next().value!\n      if (sortedItem[key] !== item[key]) {\n        wasResorted = true\n      }\n      return sortedItem\n    }\n    return item\n  })\n\n  if (!wasResorted) {\n    return null\n  }\n  return result\n}\n\nexport function getCheckedProperties<T>(\n  orderRules: ReadonlyArray<ReadonlyArray<ReadonlyArray<T>>>,\n): ReadonlyArray<T> {\n  return [...new Set<T>(orderRules.flat(2))]\n}\n"],"mappings":";AAAA,SAAgB,gBACd,MACA,YAGA,KACiB;CACjB,MAAM,kBACJ,MACA,YACkB;AAClB,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,IAClC,KAAI,QAAQ,IAAI,SAAS,KAAK,CAC5B,QAAO;AAGX,SAAO;;CAGT,MAAM,YAAY,WAAW,QAC1B,MAAM,CAAC,GAAG,OAAO;EAAC,GAAG;EAAM;EAAG;EAAE,EACjC,EAAE,CACH;CAED,MAAM,eAAe,KAAK,QACvB,SAAS,eAAe,KAAK,MAAM,UAAU,KAAK,KACpD;CAED,IAAI,cAAc;CAsBlB,MAAM,kBAnBc,aAAa,MAAM,GAAG,MAAM;EAC9C,MAAM,OAAO,EAAE,MACb,OAAO,EAAE;EACX,MAAM,eAAe,eAAe,MAAM,UAAU;EACpD,MAAM,eAAe,eAAe,MAAM,UAAU;AAGpD,MACE,iBAAiB,QACjB,iBAAiB,QACjB,iBAAiB,aAEjB,QAAO,eAAe;AAIxB,SAAO;GACP,CAEkC,QAAQ;CAC5C,MAAM,SAAS,KAAK,KAAK,SAAS;AAChC,MAAI,eAAe,KAAK,MAAM,UAAU,KAAK,MAAM;GACjD,MAAM,aAAa,gBAAgB,MAAM,CAAC;AAC1C,OAAI,WAAW,SAAS,KAAK,KAC3B,eAAc;AAEhB,UAAO;;AAET,SAAO;GACP;AAEF,KAAI,CAAC,YACH,QAAO;AAET,QAAO;;AAGT,SAAgB,qBACd,YACkB;AAClB,QAAO,CAAC,GAAG,IAAI,IAAO,WAAW,KAAK,EAAE,CAAC,CAAC"}