/*!
 * Copyright 2017 by ChartIQ, Inc.
 * All rights reserved.
 */
import React from "react";
//components
import EmptyResults from "./EmptyResults";
import AppCard from "./AppCard";
//consts
const NUMBER_OF_CARDS_PER_ROW = 2;
/**
 * The results page. Shown when filter tags are applied, search text is entered, or both.
 * @param {object} props Component props
 * @param {array} props.tags Array of tags that are applied for filtering
 * @param {array} props.cards Array of app card objects (apps that come from FDC app directory)
 * @param {func} props.addApp See AppCard.jsx
 * @param {func} props.removeApp See AppCard.jsx
 * @param {func} props.openAppShowcase See AppCard.jsx
 * @param {func} props.addTag Parent function to add a filtering tag
 */
const AppResults = (props) => {
    /**
     * Function to take the incoming apps and any filtering tags and filter the list.
     * If there are no tags, we'll use all of the cards supplied
     */
    const buildResultCards = () => {
        let cardsForShowcase = [];
        //Filter cards by tags
        if (props.tags && props.tags.length > 0) {
            cardsForShowcase = props.cards.filter((card) => {
                for (let i = 0; i < props.tags.length; i++) {
                    let tagToSearchFor = props.tags[i];
                    if (!card.tags.includes(tagToSearchFor))
                        return false;
                }
                return true;
            });
        }
        else {
            cardsForShowcase = props.cards;
        }
        return cardsForShowcase;
    };
    let cardsForShowcase = buildResultCards();
    /**
     * Function to build the table of cards based on filtered information
     */
    const getCardRows = () => {
        let cardRows = [];
        let cardsInRow = [];
        for (let i = 0; i < cardsForShowcase.length; i++) {
            let card = cardsForShowcase[i];
            let name = card.title || card.name;
            let key = `card-${i}-${name === null || name === void 0 ? void 0 : name.toLowerCase()}`;
            cardsInRow.push(React.createElement("td", { key: key },
                React.createElement(AppCard, { ...card, viewAppShowcase: props.viewAppShowcase })));
            if (cardsInRow.length === NUMBER_OF_CARDS_PER_ROW || i === cardsForShowcase.length - 1) {
                cardRows.push(cardsInRow);
                cardsInRow = [];
            }
        }
        return cardRows;
    };
    if (cardsForShowcase.length === 0)
        return React.createElement(EmptyResults, null);
    let cardRows = getCardRows();
    return (React.createElement("div", { className: "app-results" },
        React.createElement("table", { className: "app-results-table" },
            React.createElement("tbody", null, cardRows.map((row, i) => (React.createElement("tr", { key: `tablerow-${i}` }, row)))))));
};
export default AppResults;
//# sourceMappingURL=AppResults.js.map