/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import "@esri/solution-common";
import { g as getTypeKeywordProp } from "./helpers.js";
function createFilteredSubcache(sourceCache, acceptableIds) {
  const cardSubcache = [];
  sourceCache.forEach((card) => {
    if (acceptableIds.has(card.id)) {
      cardSubcache.push(card);
    }
  });
  return cardSubcache;
}
function extractFromCache(cache, start1, num) {
  if (start1 < 1 || cache.length < start1) {
    return {
      cards: [],
      paging: {
        start: start1,
        num: 0,
        nextStart: -1,
        total: cache.length
      }
    };
  }
  const end1 = Math.min(start1 + num, cache.length + 1);
  const nextStart = end1 < cache.length ? end1 : -1;
  return {
    cards: cache.slice(start1 - 1, end1 - 1),
    paging: {
      start: start1,
      num: end1 - start1,
      nextStart,
      total: cache.length
    }
  };
}
function getDeployedRelatedSolutionIds(cards, cardsToSearch) {
  const cardSolutionIds = getSolutionIds(cards);
  return getRelatedSolutions(cardSolutionIds, cardsToSearch);
}
function getRelatedSolutions(solutionIds, cards) {
  let relatedSolutions = [];
  cards.forEach(
    (card) => {
      const solutionId = getSolutionId(card);
      if (solutionId && solutionIds.indexOf(solutionId) >= 0 && card.relatedSolutions) {
        relatedSolutions = relatedSolutions.concat(card.relatedSolutions);
      }
    }
  );
  return new Set(relatedSolutions);
}
function getSolutionId(card) {
  const solutionIdPrefix = "solutionid-";
  const solutionIDList = card.typeKeywords.filter(
    (keyword) => keyword.startsWith(solutionIdPrefix)
  );
  let solutionId = null;
  if (Array.isArray(solutionIDList) && solutionIDList.length > 0) {
    solutionId = solutionIDList[0].substring(solutionIdPrefix.length);
  }
  return solutionId;
}
function getSolutionIds(cards) {
  let solutionIds = [];
  cards.forEach(
    (card) => {
      const solutionId = getSolutionId(card);
      if (solutionId) {
        solutionIds.push(solutionId);
      }
    }
  );
  solutionIds = Array.from(new Set(solutionIds));
  return solutionIds;
}
function getSolutionVersionFromTypekeyword(card) {
  const solutionVersionPrefix = "solutionversion-";
  const num = (card.typeKeywords || []).reduce((prev, cur) => {
    if (cur.startsWith(solutionVersionPrefix)) {
      prev = Number(cur.replace(solutionVersionPrefix, ""));
    }
    return prev;
  }, -1);
  return isNaN(num) ? -1 : num;
}
function sortCache(cache, sortField, sortOrder) {
  let compareFcn;
  const direction = sortOrder === "asc" ? 1 : -1;
  switch (sortField) {
    case "created":
      compareFcn = (item1, item2) => direction * (item1.createdMs < item2.createdMs ? -1 : item1.createdMs > item2.createdMs ? 1 : 0);
      break;
    case "numviews":
      compareFcn = (item1, item2) => direction * (item1.numViews < item2.numViews ? -1 : item1.numViews > item2.numViews ? 1 : 0);
      break;
    case "title":
      compareFcn = (item1, item2) => direction * (item1.title.toLowerCase() < item2.title.toLowerCase() ? -1 : item1.title.toLowerCase() > item2.title.toLowerCase() ? 1 : 0);
      break;
    default:
      compareFcn = () => 0;
      break;
  }
  cache.sort(compareFcn);
  return cache;
}
function convertTitleToUrlForm(title) {
  return title.toLowerCase().replace(/\s+/g, "-");
}
function findSolutionByTitleOrId(cards, solutionTitleOrId) {
  return cards.find((card) => {
    const templateUrlFormTitle = convertTitleToUrlForm(card.title);
    return templateUrlFormTitle === solutionTitleOrId || solutionTitleOrId === card.id;
  });
}
function findSolutionByTypeKeywordSolutionId(cards, solutionId) {
  const cSolutionIdPrefix = "solutionid-";
  return cards.find((card) => {
    const cardSolutionId = getTypeKeywordProp(card.typeKeywords, cSolutionIdPrefix);
    return cardSolutionId === solutionId;
  });
}
function findSolutionTitleByDeployCommonId(cards, id) {
  const card = cards.find((card2) => id === card2.deployCommonId);
  return convertTitleToUrlForm(card.title);
}
export {
  findSolutionTitleByDeployCommonId as a,
  getDeployedRelatedSolutionIds as b,
  convertTitleToUrlForm as c,
  createFilteredSubcache as d,
  extractFromCache as e,
  findSolutionByTypeKeywordSolutionId as f,
  getSolutionVersionFromTypekeyword as g,
  findSolutionByTitleOrId as h,
  sortCache as s
};
