import React, { useMemo } from "react";
import { useTranslation } from "react-i18next";
import * as styles from "../styles";
import { primitiveUtils } from "~/utils";
import { ChangeIndicator } from "~/sdks/NeighborhoodMarketTrends/components/ChangeIndicator";
import { InfoPopover } from "~/sdks/NeighborhoodMarketTrends/components/InfoPopover";
import { useNeighborhoodMarketTrends } from "~/sdks/NeighborhoodMarketTrends/context";
import * as Graphs from "~/sdks/NeighborhoodMarketTrends/components/Graphs";
export const MedianAskingPrice = () => {
    const { t } = useTranslation("neighborhoodMarketTrends");
    const { marketActivity, appearance } = useNeighborhoodMarketTrends();
    const medianAskingPrices = useMemo(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
        if (!marketActivity) {
            return null;
        }
        return {
            current: (_e = (_d = (_c = (_b = (_a = marketActivity.market_activity_by_property_type) === null || _a === void 0 ? void 0 : _a.all_residential_properties.intervals["30d"]) === null || _b === void 0 ? void 0 : _b.market_activity) === null || _c === void 0 ? void 0 : _c.stats_at_period_end) === null || _d === void 0 ? void 0 : _d.home_asking_prices) === null || _e === void 0 ? void 0 : _e.median_price,
            previous: (_l = (_k = (_j = (_h = (_g = (_f = marketActivity.market_activity_by_property_type) === null || _f === void 0 ? void 0 : _f.all_residential_properties) === null || _g === void 0 ? void 0 : _g.intervals["31-60d"]) === null || _h === void 0 ? void 0 : _h.market_activity) === null || _j === void 0 ? void 0 : _j.stats_at_period_end) === null || _k === void 0 ? void 0 : _k.home_asking_prices) === null || _l === void 0 ? void 0 : _l.median_price,
        };
    }, [marketActivity]);
    const monthlyChange = useMemo(() => {
        if (!medianAskingPrices ||
            !medianAskingPrices.current ||
            !medianAskingPrices.previous) {
            return null;
        }
        return ((medianAskingPrices.current - medianAskingPrices.previous) /
            medianAskingPrices.previous);
    }, [medianAskingPrices]);
    if (!medianAskingPrices) {
        return null;
    }
    return (React.createElement("div", { className: styles.container },
        React.createElement("div", { className: styles.summary },
            React.createElement("div", null,
                React.createElement("div", { className: styles.header },
                    React.createElement("h3", { className: styles.title },
                        t("graphs.medianAskingPrice.title"),
                        React.createElement(InfoPopover, { change: monthlyChange }, t("graphs.medianAskingPrice.disclaimer"))),
                    React.createElement("h4", { className: styles.amount }, medianAskingPrices.current &&
                        primitiveUtils.number.formatCurrency(medianAskingPrices.current)))),
            React.createElement(ChangeIndicator, { yearlyChange: monthlyChange, fieldTitle: t("graphs.medianAskingPrice.title"), disclaimer: t("graphs.medianAskingPrice.changeDisclaimer") })),
        React.createElement(Graphs.Root, { colors: appearance === null || appearance === void 0 ? void 0 : appearance.colors },
            React.createElement(Graphs.MedianAskingPrice, { isIncrease: !monthlyChange || monthlyChange >= 0 }))));
};
//# sourceMappingURL=index.js.map