"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExistingCardPicker = void 0;
const contexts_1 = require("../../../../contexts/index.js");
const react_1 = __importStar(require("react"));
const MemberCardItem_1 = require("../../../pages/Account/Card/MemberCardItem");
const LoggedInUserHook_1 = require("../../../../hooks/LoggedInUserHook");
/*
    Component assumes that parent checks if member is already logged in
*/
const ExistingCardPicker = (props) => {
    const { user } = (0, LoggedInUserHook_1.useMember)();
    const { cards } = (0, react_1.useContext)(contexts_1.RoomstayMemberContext);
    const [selectedCardId, setSelectedCardId] = (0, react_1.useState)();
    (0, react_1.useEffect)(() => {
        if ((user === null || user === void 0 ? void 0 : user.defaultCard) && cards) {
            for (const card of cards) {
                if (user.defaultCard === card.cardId) {
                    setSelectedCardId(user.defaultCard);
                    return;
                }
            }
        }
        if (cards === null || cards === void 0 ? void 0 : cards.length) {
            setSelectedCardId(cards[0].cardId);
        }
    }, [user, cards]);
    const onSelectedCardChanged = (newCard) => {
        setSelectedCardId(newCard);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", null, cards === null || cards === void 0 ? void 0 : cards.map((card, index) => {
            const onClickCard = () => {
                onSelectedCardChanged(card.cardId);
            };
            return react_1.default.createElement(MemberCardItem_1.MemberCardItem, { key: index, selected: selectedCardId === card.cardId, onClick: onClickCard, item: card });
        }))));
};
exports.ExistingCardPicker = ExistingCardPicker;
//# sourceMappingURL=ExistingCardPicker.js.map