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 MedianSalePriceSQFT = () => {
    const { t } = useTranslation("neighborhoodMarketTrends");
    const { marketActivity, appearance } = useNeighborhoodMarketTrends();
    const medianSalesPriceSQFT = 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["12m"]) === null || _b === void 0 ? void 0 : _b.market_activity) === null || _c === void 0 ? void 0 : _c.stats_over_period) === null || _d === void 0 ? void 0 : _d.home_selling_prices) === null || _e === void 0 ? void 0 : _e.median_price_per_square_foot,
            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["13-24m"]) === null || _h === void 0 ? void 0 : _h.market_activity) === null || _j === void 0 ? void 0 : _j.stats_over_period) === null || _k === void 0 ? void 0 : _k.home_selling_prices) === null || _l === void 0 ? void 0 : _l.median_price_per_square_foot,
        };
    }, [marketActivity]);
    const yearlyChange = useMemo(() => {
        if (!medianSalesPriceSQFT ||
            !medianSalesPriceSQFT.previous ||
            !medianSalesPriceSQFT.current) {
            return null;
        }
        return ((medianSalesPriceSQFT.current - medianSalesPriceSQFT.previous) /
            medianSalesPriceSQFT.previous);
    }, [medianSalesPriceSQFT]);
    if (!medianSalesPriceSQFT) {
        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.medianSalePricePerSqft.title"),
                        React.createElement(InfoPopover, { change: yearlyChange }, t("graphs.medianSalePricePerSqft.disclaimer"))),
                    React.createElement("h4", { className: styles.amount }, medianSalesPriceSQFT.current &&
                        primitiveUtils.number.formatCurrency(medianSalesPriceSQFT.current)))),
            React.createElement(ChangeIndicator, { yearlyChange: yearlyChange, fieldTitle: t("graphs.medianSalePricePerSqft.title"), disclaimer: t("graphs.medianSalePricePerSqft.changeDisclaimer") })),
        React.createElement(Graphs.Root, { colors: appearance === null || appearance === void 0 ? void 0 : appearance.colors },
            React.createElement(Graphs.MedianSalePriceSQFT, { isIncrease: !yearlyChange || yearlyChange >= 0 }))));
};
//# sourceMappingURL=index.js.map