"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reducer = exports.getFinishedStepsInitialStates = exports.getActiveSectionsInitialStates = exports.getActiveProductsInitialStates = void 0; var _helpers = require("./helpers"); var _types = require("./types"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ const reducer = (state, action) => { if (action.type === _types.GetStartedPageActions.ToggleProduct) { const activeProducts = new Set([...state.activeProducts]); if (activeProducts.has(action.payload.section)) { activeProducts.delete(action.payload.section); } else { activeProducts.add(action.payload.section); } const { activeSections, totalStepsLeft, totalActiveSteps } = (0, _helpers.setupActiveSections)(state.finishedSteps, activeProducts); return { ...state, activeProducts, activeSections, totalStepsLeft, totalActiveSteps }; } if (action.type === _types.GetStartedPageActions.AddFinishedStep) { const finishedSteps = { ...state.finishedSteps, [action.payload.cardId]: state.finishedSteps[action.payload.cardId] ? new Set([...state.finishedSteps[action.payload.cardId]]) : new Set() }; if (action.type === _types.GetStartedPageActions.AddFinishedStep) { finishedSteps[action.payload.cardId].add(action.payload.stepId); } const { activeSections, totalStepsLeft, totalActiveSteps } = (0, _helpers.updateActiveSections)({ activeProducts: state.activeProducts, activeSections: state.activeSections, cardId: action.payload.cardId, finishedSteps, sectionId: action.payload.sectionId }); return { ...state, finishedSteps, activeSections, totalStepsLeft, totalActiveSteps }; } if (action.type === _types.GetStartedPageActions.RemoveFinishedStep) { const finishedSteps = { ...state.finishedSteps, [action.payload.cardId]: state.finishedSteps[action.payload.cardId] ? new Set([...state.finishedSteps[action.payload.cardId]]) : new Set() }; if (action.type === _types.GetStartedPageActions.RemoveFinishedStep) { finishedSteps[action.payload.cardId].delete(action.payload.stepId); } const { activeSections, totalStepsLeft, totalActiveSteps } = (0, _helpers.updateActiveSections)({ activeProducts: state.activeProducts, activeSections: state.activeSections, cardId: action.payload.cardId, finishedSteps, sectionId: action.payload.sectionId }); return { ...state, finishedSteps, activeSections, totalStepsLeft, totalActiveSteps }; } if (action.type === _types.GetStartedPageActions.ToggleExpandedCardStep && action.payload.isCardExpanded != null) { var _state$expandedCardSt, _state$expandedCardSt2; return { ...state, expandedCardSteps: { ...state.expandedCardSteps, [action.payload.cardId]: { expandedSteps: (_state$expandedCardSt = (_state$expandedCardSt2 = state.expandedCardSteps[action.payload.cardId]) === null || _state$expandedCardSt2 === void 0 ? void 0 : _state$expandedCardSt2.expandedSteps) !== null && _state$expandedCardSt !== void 0 ? _state$expandedCardSt : [], isExpanded: action.payload.isCardExpanded } } }; } if (action.type === _types.GetStartedPageActions.ToggleExpandedCardStep && action.payload.isStepExpanded != null) { var _ref, _state$expandedCardSt3, _state$expandedCardSt4; const expandedSteps = new Set((_ref = [...((_state$expandedCardSt3 = state.expandedCardSteps[action.payload.cardId]) === null || _state$expandedCardSt3 === void 0 ? void 0 : _state$expandedCardSt3.expandedSteps)]) !== null && _ref !== void 0 ? _ref : []); if (action.payload.isStepExpanded === true && action.payload.stepId) { expandedSteps.add(action.payload.stepId); } if (action.payload.isStepExpanded === false && action.payload.stepId) { expandedSteps.delete(action.payload.stepId); } return { ...state, expandedCardSteps: { ...state.expandedCardSteps, [action.payload.cardId]: { expandedSteps: [...expandedSteps], isExpanded: (_state$expandedCardSt4 = state.expandedCardSteps[action.payload.cardId]) === null || _state$expandedCardSt4 === void 0 ? void 0 : _state$expandedCardSt4.isExpanded } } }; } return state; }; exports.reducer = reducer; const getFinishedStepsInitialStates = ({ finishedSteps }) => { const initialStates = Object.entries(finishedSteps).reduce((acc, [cardId, stepIdsByCard]) => { if (stepIdsByCard) { acc[cardId] = new Set(stepIdsByCard); } return acc; }, {}); return initialStates; }; exports.getFinishedStepsInitialStates = getFinishedStepsInitialStates; const getActiveProductsInitialStates = ({ activeProducts }) => new Set(activeProducts); exports.getActiveProductsInitialStates = getActiveProductsInitialStates; const getActiveSectionsInitialStates = ({ activeProducts, finishedSteps }) => (0, _helpers.setupActiveSections)(finishedSteps, activeProducts); exports.getActiveSectionsInitialStates = getActiveSectionsInitialStates;