import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { useAppDispatch, useAppSelector } from 'src/app/redux/store';
import { track } from 'src/lib/common/helpers/tracking';
import { getDoctorTypeText, getMapsDirectionsLink, getRoundedDistanceWithUnit, formatPhoneNumber, hasDynamicProgram, } from 'src/lib/common/helpers';
import Button from '../Button';
import css from './Maps.module.scss';
import { _cx } from 'src/utility/class';
import { HfdLogo } from 'src/lib/utils';
var cx = _cx(css);
var InfoWindow = function (_a) {
    var _b, _c, _d;
    var marker = _a.marker;
    var dispatch = useAppDispatch();
    var _e = useAppSelector(function (state) { return state.config.config; }), language = _e.language, doctorProfileUrl = _e.doctorProfileUrl, formatResultPhone = _e.formatResultPhone, customAppointmentUrl = _e.customAppointmentUrl, enableHeartlandButtonSwap = _e.enableHeartlandButtonSwap, scheduleAppointmentUrl = _e.scheduleAppointmentUrl, zocdocScheduleAppointmentUrl = _e.zocdocScheduleAppointmentUrl, country = _e.country, enableZocdoc = _e.enableZocdoc, tierPins = _e.tierPins, enableNexHealth = _e.enableNexHealth, nexHealthScheduleAppointmentUrl = _e.nexHealthScheduleAppointmentUrl, appointmentButtonSecquence = _e.appointmentButtonSecquence;
    var vivera = useAppSelector(function (state) { return state.filter.filters.vivera; });
    var program = useAppSelector(function (state) { return state.common.programEnabled; });
    var t = useAppSelector(function (state) { return state.config.config.languageData[language]; });
    var distance = useMemo(function () { var _a; return getRoundedDistanceWithUnit((_a = marker === null || marker === void 0 ? void 0 : marker.Distance) !== null && _a !== void 0 ? _a : 0); }, [marker]);
    var customRAURL = customAppointmentUrl.find(function (it) {
        return it.docID === marker.DocID;
    });
    var isHeartland = enableHeartlandButtonSwap && marker.IsHeartLandOLS;
    var isHfdEnabled = document.body.classList.contains('hfdEnable') &&
        hasDynamicProgram(marker, 'is_hfd_prequal') &&
        marker.HfdPrequalUrl;
    var tierData = tierPins.find(function (pins) { return pins.segmentCode === marker.SegmentCode; });
    var isZocdocEnabled = hasDynamicProgram(marker, 'is_zocdoc') && enableZocdoc;
    var isNexHealthEnabled = hasDynamicProgram(marker, 'is_nexhealth') && enableNexHealth;
    var defaultButtonSequence = ['zocdoc', 'nexhealth', 'heartland', 'customRAUrl', 'hfd', 'default'];
    var buttonSequence = (appointmentButtonSecquence === null || appointmentButtonSecquence === void 0 ? void 0 : appointmentButtonSecquence.length)
        ? appointmentButtonSecquence
        : defaultButtonSequence;
    var buttonConditions = {
        hfd: !!isHfdEnabled,
        customRAUrl: !!(customRAURL === null || customRAURL === void 0 ? void 0 : customRAURL.url),
        heartland: !!isHeartland,
        zocdoc: !!isZocdocEnabled,
        nexhealth: !!isNexHealthEnabled,
        default: true,
    };
    var activeButton = buttonSequence.find(function (key) { return buttonConditions[key]; }) || 'default';
    var defaultProfileUrl = "".concat(doctorProfileUrl, "?profile=").concat(marker.DocID, "&requestAppointment=true").concat(vivera ? "&vivera=yes" : "").concat(program ? "&program=".concat(program) : "");
    var renderAppointmentButton = function () {
        switch (activeButton) {
            case 'zocdoc':
                return (_jsx(Button, { styleType: "small", width: "50%", bgColor: "#E10098", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_serp',
                            link_text: t.schedule_online_zocdoc_button,
                            link_url: "".concat(zocdocScheduleAppointmentUrl, "?id=").concat(marker.DocID),
                            profile_number: marker.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(marker.DoctorType),
                            is_ols: true,
                            ols_api: 'Zocdoc',
                            hfd_prequal_eligible: hasDynamicProgram(marker, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(zocdocScheduleAppointmentUrl, "?id=").concat(marker.DocID);
                    }, children: t.schedule_online_zocdoc_button }));
            case 'nexhealth':
                return (_jsx(Button, { styleType: "small", width: "50%", bgColor: "#E10098", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_serp',
                            link_text: t.schedule_online_nexhealth_button,
                            link_url: "".concat(nexHealthScheduleAppointmentUrl, "?id=").concat(marker.DocID),
                            profile_number: marker.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(marker.DoctorType),
                            is_ols: true,
                            ols_api: 'NexHealth',
                            hfd_prequal_eligible: hasDynamicProgram(marker, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(nexHealthScheduleAppointmentUrl, "?id=").concat(marker.DocID);
                    }, children: t.schedule_online_nexhealth_button }));
            case 'heartland':
                return (_jsx(Button, { styleType: "small", width: "50%", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_serp',
                            link_text: t.result_schedule_appointment_button,
                            link_url: "".concat(scheduleAppointmentUrl, "?id=").concat(marker.DocID),
                            profile_number: marker.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(marker.DoctorType),
                            is_ols: marker.IsHeartLandOLS,
                            ols_api: 'Heartland',
                            hfd_prequal_eligible: hasDynamicProgram(marker, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(scheduleAppointmentUrl, "?id=").concat(marker.DocID);
                    }, children: t.result_schedule_appointment_button }));
            case 'customRAUrl':
                return (_jsx(Button, { styleType: "small", width: "50%", onClick: function () {
                        track('doc_loc_custom_schedule_online_click', {
                            profile_number: marker.DocID,
                            link_url: customRAURL.url,
                        });
                        window.open(customRAURL.url, '_blank');
                    }, children: t.schedule_online }));
            case 'hfd':
                return (_jsx(Button, { styleType: "smallWithIcon", buttonStyle: { margin: '0 0 0 8px' }, icon: [_jsx(HfdLogo, { width: "24px" }), _jsx(HfdLogo, { color: "white", width: "24px" })], width: "50%", onClick: function () {
                        track('doc_loc_hfd_cta_click', {
                            link_location: 'doc_loc_serp',
                            link_text: t.get_prequalified,
                            link_url: marker === null || marker === void 0 ? void 0 : marker.HfdPrequalUrl,
                            profile_number: marker.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(marker.DoctorType),
                        });
                        window.open("".concat(t.fhd_page_url, "?id=").concat(marker === null || marker === void 0 ? void 0 : marker.DocID, "&iframeurl=").concat((marker === null || marker === void 0 ? void 0 : marker.HfdPrequalUrl) && encodeURIComponent(marker === null || marker === void 0 ? void 0 : marker.HfdPrequalUrl)), '_self');
                    }, children: t.get_prequalified }));
            default:
                return (_jsx(Button, { styleType: "small", width: "50%", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_serp',
                            link_text: t.result_request_appointment_button,
                            link_url: defaultProfileUrl,
                            profile_number: marker.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(marker.DoctorType),
                            is_ols: marker.IsHeartLandOLS ||
                                hasDynamicProgram(marker, 'is_zocdoc') ||
                                hasDynamicProgram(marker, 'is_nexhealth'),
                            ols_api: marker.IsHeartLandOLS
                                ? 'Heartland'
                                : hasDynamicProgram(marker, 'is_zocdoc')
                                    ? 'Zocdoc'
                                    : hasDynamicProgram(marker, 'is_nexhealth')
                                        ? 'NexHealth'
                                        : undefined,
                            hfd_prequal_eligible: hasDynamicProgram(marker, 'is_hfd_prequal'),
                        });
                        window.location.href = defaultProfileUrl;
                    }, children: t.result_request_appointment_button }));
        }
    };
    return (_jsxs("div", { className: "map-info-window ".concat(cx('info-window-wrap')), "data-is-heartland-ols": marker.IsHeartLandOLS, "data-is-hfd-prequal": hasDynamicProgram(marker, 'is_hfd_prequal'), "data-doc-id": marker.DocID, children: [_jsxs("div", { className: cx('content'), children: [_jsxs("div", { children: [_jsx("h3", { children: ((_b = marker.PreferredName) === null || _b === void 0 ? void 0 : _b.toUpperCase()) ||
                                    ((_c = marker.PracticeName) === null || _c === void 0 ? void 0 : _c.toUpperCase()) ||
                                    ((_d = marker.FullName) === null || _d === void 0 ? void 0 : _d.toUpperCase()) }), _jsxs("p", { className: cx('textSmall'), children: [getDoctorTypeText(marker.DoctorType), marker.HasBio && (_jsxs(_Fragment, { children: [' ', "|", ' ', _jsx("a", { className: cx('profile-link textSmall'), href: "".concat(doctorProfileUrl, "?profile=").concat(marker.DocID).concat(vivera ? "&vivera=yes" : "").concat(program ? "&program=".concat(program) : ""), children: t.result_profile_button })] }))] })] }), _jsxs("div", { className: cx('textSmall'), children: [_jsxs("span", { className: cx('distance-icon'), children: [' ', _jsx("svg", { width: "9", height: "10", viewBox: "0 0 9 10", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: _jsx("path", { d: "M4.5 5C4.775 5 5.0105 4.902 5.2065 4.706C5.40217 4.51033 5.5 4.275 5.5 4C5.5 3.725 5.40217 3.4895 5.2065 3.2935C5.0105 3.09783 4.775 3 4.5 3C4.225 3 3.98967 3.09783 3.794 3.2935C3.598 3.4895 3.5 3.725 3.5 4C3.5 4.275 3.598 4.51033 3.794 4.706C3.98967 4.902 4.225 5 4.5 5ZM4.5 10C3.15833 8.85833 2.15633 7.79783 1.494 6.8185C0.831333 5.8395 0.5 4.93333 0.5 4.1C0.5 2.85 0.902167 1.85417 1.7065 1.1125C2.5105 0.370833 3.44167 0 4.5 0C5.55833 0 6.4895 0.370833 7.2935 1.1125C8.09783 1.85417 8.5 2.85 8.5 4.1C8.5 4.93333 8.16883 5.8395 7.5065 6.8185C6.84383 7.79783 5.84167 8.85833 4.5 10Z", fill: "#0067AC" }) })] }), "".concat(distance), " ", t.result_distance_away, "\u00A0(", _jsx("a", { className: cx('profile-link textSmall'), href: getMapsDirectionsLink(marker), rel: "noreferrer noopener", target: "_blank", children: t.result_directions }), ")"] }), _jsxs("div", { className: cx('textSmall'), children: [_jsx("div", { children: "".concat(marker.Line1).concat(marker.Line2 ? ", ".concat(marker.Line2) : "") }), "".concat(marker.City, ", ").concat(marker.State, ", ").concat(marker.PostalCode)] })] }), _jsxs("div", { className: "flex", children: [_jsx("a", { className: cx('phone-btn'), href: "tel:".concat(marker.OfficePhone || marker.Phone), onClick: function () {
                            return track('doc_loc_practice_phone_click', {
                                link_location: 'doc_loc_serp',
                                link_text: formatPhoneNumber(marker.OfficePhone || marker.Phone),
                                link_url: "tel:".concat(marker.OfficePhone || marker.Phone),
                                profile_number: marker.DocID,
                                profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                                profile_designation: getDoctorTypeText(marker.DoctorType),
                            });
                        }, children: _jsx("div", { className: cx('phone'), children: _jsx("span", { children: "".concat(formatResultPhone
                                    ? formatPhoneNumber(marker.OfficePhone || marker.Phone)
                                    : marker.OfficePhone || marker.Phone) }) }) }), renderAppointmentButton()] })] }));
};
export default InfoWindow;
