"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = RoomFilterPriceSlider;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const Currency_1 = __importDefault(require("../../../generic/Currency"));
const SmallSpinner_1 = __importDefault(require("../../../generic/loader/SmallSpinner"));
const Slider_1 = require("../../../generic/Slider/Slider");
const Text_1 = __importStar(require("../../../generic/Text"));
const BudgetFilter_1 = __importDefault(require("../../../../models/Room/Filters/BudgetFilter"));
const Color_1 = require("../../../../util/Color");
function RoomFilterPriceSlider(props) {
    var _a, _b, _c, _d, _e;
    const { t } = (0, react_i18next_1.useTranslation)();
    const [rangeDefaults, setRangeDefaults] = (0, react_1.useState)([(_b = (_a = props.filter) === null || _a === void 0 ? void 0 : _a.getMin()) !== null && _b !== void 0 ? _b : 0, (_d = (_c = props.filter) === null || _c === void 0 ? void 0 : _c.getMax()) !== null && _d !== void 0 ? _d : 0]);
    const [range, setRange] = (0, react_1.useState)(rangeDefaults);
    const [peaksReached, setPeaksReached] = (0, react_1.useState)(null);
    (0, react_1.useEffect)(() => {
        var _a, _b, _c, _d, _e, _f;
        const min = ((_a = props.roomList) === null || _a === void 0 ? void 0 : _a.length)
            ? props.roomList.reduce((total, room) => {
                const roomLowest = room.getLowestPrice(true);
                return total < roomLowest && total ? total : roomLowest;
            }, 0)
            : 0;
        const max = ((_b = props.roomList) === null || _b === void 0 ? void 0 : _b.length)
            ? props.roomList.reduce((total, room) => {
                const roomHighest = room.getHighestPrice(true);
                return total > roomHighest && total ? total : roomHighest;
            }, 0)
            : 0;
        setRangeDefaults([min, max]);
        setRange([(_d = (_c = props.filter) === null || _c === void 0 ? void 0 : _c.getMin()) !== null && _d !== void 0 ? _d : min, (_f = (_e = props.filter) === null || _e === void 0 ? void 0 : _e.getMax()) !== null && _f !== void 0 ? _f : max]);
    }, [props.roomList]);
    (0, react_1.useEffect)(() => {
        const rangeDifference = rangeDefaults[1] - rangeDefaults[0];
        // Basically we're checking if the RIGHT slider (max) is within 1.2x of the smallest value
        // and if LEFT slider (min) is within .8 of the largest value. This will hopefully allow us to transform the label
        // away from the edges, to avoid overlapping
        if (range[1] < rangeDefaults[0] + rangeDifference * 0.3) {
            setPeaksReached('left');
        }
        else if (range[0] > rangeDefaults[1] - rangeDifference * 0.3) {
            setPeaksReached('right');
        }
        else {
            setPeaksReached(null);
        }
    }, [range]);
    const currencyObj = (value, disableLoading = false) => {
        return (react_1.default.createElement(Currency_1.default, { hideDecimals: true, disableLoading: true }, value));
    };
    const budgetMarks = [
        {
            label: (react_1.default.createElement("span", { className: "be-slider-label --start", style: { color: Color_1.Color.Grey } }, currencyObj(rangeDefaults[0]))),
            value: rangeDefaults[0],
        },
        {
            label: (react_1.default.createElement("span", { className: "be-slider-label", style: { color: Color_1.Color.Grey } }, currencyObj(rangeDefaults[1]))),
            value: rangeDefaults[1],
        },
        {
            label: (react_1.default.createElement("span", { className: "be-slider-label --main" },
                currencyObj(range[0]),
                " - ",
                currencyObj(range[1]))),
            value: (range[1] - range[0]) / 2 + range[0],
        },
    ];
    const handleBudgetChange = (newValue) => {
        setRange(newValue.map((value) => value.value));
        props.onChange(new BudgetFilter_1.default(newValue[0].value, newValue[1].value));
    };
    const reset = () => {
        setRange(rangeDefaults);
        props.onChange(null);
    };
    const resetOnClickHandler = () => {
        return reset();
    };
    const markers = [
        {
            isVisible: true,
            value: range[0],
        },
        {
            isVisible: true,
            value: range[1],
        },
        {
            isVisible: false,
            label: (react_1.default.createElement(react_1.default.Fragment, null,
                currencyObj(range[0]),
                " - ",
                currencyObj(range[1]))),
            value: range[0] + (range[1] - range[0]) / 2,
        },
    ];
    const backgroundMarkers = [
        {
            isVisible: true,
            value: rangeDefaults[0],
            label: currencyObj(rangeDefaults[0]),
            isEdge: 'start',
        },
        {
            isVisible: true,
            value: rangeDefaults[1],
            label: currencyObj(rangeDefaults[1]),
            isEdge: 'end',
        },
    ];
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "u-flex" },
            react_1.default.createElement("div", null,
                react_1.default.createElement(Text_1.default, { inline: true, type: Text_1.TextType.Small },
                    react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Room.Filter.Budget))),
                react_1.default.createElement(Text_1.default, { inline: true, type: Text_1.TextType.Small },
                    "/",
                    t(Translation_1.Translation.Misc.PerNight))),
            react_1.default.createElement("div", null,
                react_1.default.createElement(BEButton_1.default, { size: "tiny", isText: true, disabled: !props.filter, onClick: resetOnClickHandler }, t(Translation_1.Translation.Misc.Clear)))),
        ((_e = props.roomList) === null || _e === void 0 ? void 0 : _e.length) > 0 ? (react_1.default.createElement("div", { className: (0, classnames_1.default)('roomstay-slider', {
                '--min-limit': peaksReached === 'left',
                '--max-limit': peaksReached === 'right',
            }) },
            react_1.default.createElement(Slider_1.Slider, { min: rangeDefaults[0], max: rangeDefaults[1], onChange: handleBudgetChange, 
                // onChangeCommitted={handleBudgetCommittedChange}
                // aria-labelledby="track-false-range-slider"
                // value={range}
                markers: markers, backgroundMarkers: backgroundMarkers }))) : (react_1.default.createElement("div", { className: "u-marg-top-bottom u-flex justify-content-center" },
            react_1.default.createElement(SmallSpinner_1.default, null)))));
}
//# sourceMappingURL=RoomFilterPriceSlider.js.map