"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceStatusProvider = exports.useFilteredServiceStatus = exports.filterServiceStatusIncidents = exports.useServiceStatus = void 0;
const react_1 = __importStar(require("react"));
const api_1 = __importDefault(require("./api"));
const xstate_1 = require("xstate");
const react_2 = require("@xstate/react");
const machine_1 = require("./machine");
const ledger_components_1 = require("./ledger-components");
const ServiceStatusContext = (0, react_1.createContext)({
    incidents: [],
    isLoading: false,
    lastUpdateTime: undefined,
    error: undefined,
    updateData: () => Promise.resolve(),
    context: { tickers: [] },
});
function useServiceStatus() {
    return (0, react_1.useContext)(ServiceStatusContext);
}
exports.useServiceStatus = useServiceStatus;
function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()[\]\\]/g, "\\$&"); // $& means the whole matched string
}
function sanitizeName(name) {
    return name.toLowerCase().trim().replace(/\s+/g, " "); // collapse multiple spaces
}
/**
 * Filters service status incidents based on related tickers or Ledger components.
 *
 * ## Behavior:
 * - If there are no `tickers` or no `incidents`, returns an empty list.
 * - For each incident:
 *   1. ✅ If the incident has no components → keep it (always relevant).
 *   2. ✅ If at least one component name matches a known **Ledger component** → keep it.
 *   3. ✅ If at least one component name contains a **currency ticker** → keep it.
 *   4. ❌ Otherwise, the incident is discarded.
 *
 * @param {Incident[]} [incidents=[]] - List of incidents to filter.
 * @param {string[]} [tickers=[]] - List of currency tickers to match against incident components.
 * @param {EntryPoint} [entryPoint] - Entry point where the incidents are displayed (specifically for Ledger components)
 * @returns {Incident[]} The list of incidents relevant to the provided tickers or Ledger components.
 */
function filterServiceStatusIncidents(incidents = [], tickers = [], entryPoint) {
    if (!incidents.length)
        return [];
    if (!tickers.length)
        return [];
    const tickerRegex = tickers.length
        ? new RegExp(`\\b(${tickers.map(escapeRegExp).join("|")})\\b`, "i")
        : null;
    const ledgerComponentsSet = new Set(ledger_components_1.LEDGER_COMPONENTS.map(component => sanitizeName(component)));
    return incidents.filter(({ components }) => {
        // Keep global incidents with no components
        if (!components?.length)
            return true;
        return components.some(({ name }) => {
            const sanitizedName = sanitizeName(name);
            // Show Ledger components only for Notification Center
            if (entryPoint === "notifications" && ledgerComponentsSet.has(sanitizedName)) {
                return true;
            }
            // Show coin/ticker-specific components for flows
            if (tickerRegex && tickerRegex.test(sanitizedName)) {
                return true;
            }
            return false;
        });
    });
}
exports.filterServiceStatusIncidents = filterServiceStatusIncidents;
// filter out service status incidents by given currencies or fallback on context currencies
function useFilteredServiceStatus(filters) {
    const stateData = (0, react_1.useContext)(ServiceStatusContext);
    const { incidents, context } = stateData;
    const filteredIncidents = (0, react_1.useMemo)(() => {
        return filterServiceStatusIncidents(incidents, filters?.tickers || context?.tickers, filters?.entryPoint);
    }, [incidents, filters?.tickers, context?.tickers, filters?.entryPoint]);
    return { ...stateData, incidents: filteredIncidents };
}
exports.useFilteredServiceStatus = useFilteredServiceStatus;
const ServiceStatusProvider = ({ children, autoUpdateDelay, networkApi = api_1.default, context, }) => {
    const fetchData = (0, react_1.useCallback)(async () => {
        const serviceStatusSummary = await networkApi.fetchStatusSummary();
        return {
            incidents: serviceStatusSummary.incidents,
            updateTime: Date.now(),
        };
    }, [networkApi]);
    const [state, send] = (0, react_2.useMachine)(machine_1.serviceStatusMachine.provide({
        actors: {
            fetchData: (0, xstate_1.fromPromise)(fetchData),
        },
        delays: {
            AUTO_UPDATE_DELAY: autoUpdateDelay,
        },
    }));
    const api = (0, react_1.useMemo)(() => ({
        updateData: async () => {
            send({
                type: "UPDATE_DATA",
            });
        },
    }), [send]);
    const value = { ...(state.context || {}), ...api, context };
    return react_1.default.createElement(ServiceStatusContext.Provider, { value: value }, children);
};
exports.ServiceStatusProvider = ServiceStatusProvider;
//# sourceMappingURL=index.js.map