var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useCallback, useMemo, useState } from 'react';
import { useAppDispatch, useAppSelector } from 'src/app/redux/store';
import { track } from 'src/lib/common/helpers/tracking';
import { setClickedMarkerId, setHoveredMarkerId, } from 'src/app/redux/slices/common';
import { formatPhoneNumber, getDoctorTypeText, getProfileUrl, getRoundedDistanceWithUnit, hasDynamicProgram, } from 'src/lib/common/helpers';
import Modal from 'src/components/Modal/DetailModal/ModalModule';
import ModalModule from 'src/components/Modal/ModalModule';
import { getLink, renderAddress } from '../../ResultPage';
import Button from 'src/components/Button';
import css from './DoctorCard.module.scss';
import { _cx } from 'src/utility/class';
import { getSocialIconProps, SocialIconsContainer } from 'src/components/DoctorProfile/lib';
import { HfdLogo } from 'src/lib/utils';
var cx = _cx(css);
var DoctorCard = React.memo(function DoctorCard(_a) {
    var _b, _c;
    var item = _a.item, mapView = _a.mapView, isMobile = _a.isMobile, clickedMarkerId = _a.clickedMarkerId;
    var dispatch = useAppDispatch();
    var _d = useAppSelector(function (state) { return state.config.config; }), language = _d.language, languageData = _d.languageData, resultProfileImage = _d.resultProfileImage, formatResultPhone = _d.formatResultPhone, pageLimit = _d.resultsPerPage, doctorProfileUrl = _d.doctorProfileUrl, isLatam = _d.isLatam, hideLabels = _d.hideLabels, modalsData = _d.modalsData, tierPins = _d.tierPins, customAppointmentUrl = _d.customAppointmentUrl, enableHeartlandButtonSwap = _d.enableHeartlandButtonSwap, scheduleAppointmentUrl = _d.scheduleAppointmentUrl, useDocLoc3O = _d.useDocLoc3O, socialIcons = _d.socialIcons, showSocialIcons = _d.showSocialIcons, country = _d.country, zocdocScheduleAppointmentUrl = _d.zocdocScheduleAppointmentUrl, enableZocdoc = _d.enableZocdoc, enableNexHealth = _d.enableNexHealth, nexHealthScheduleAppointmentUrl = _d.nexHealthScheduleAppointmentUrl, appointmentButtonSecquence = _d.appointmentButtonSecquence;
    var programEnabled = useAppSelector(function (state) { return state.common.programEnabled; });
    var vivera = useAppSelector(function (state) { return state.filter.filters.vivera; });
    var modaldata = modalsData.find(function (item) { return item.id === 'tierLevel'; });
    var _e = __read(useState(false), 2), showHfdModal = _e[0], setShowHfdModal = _e[1];
    var hfdmodaldata = modalsData.find(function (item) { return item.id === 'hfd_financing_modal'; });
    var _f = __read(useState(false), 2), showModal = _f[0], setShowModal = _f[1];
    var _g = __read(useState(), 2), modalData = _g[0], setModalData = _g[1];
    var distance = useMemo(function () { return getRoundedDistanceWithUnit(item.Distance); }, [item]);
    var showVip = useMemo(function () { return !hideLabels || (hideLabels && !hideLabels.vip); }, [hideLabels]);
    var t = languageData[language];
    var _h = __read(useState(false), 2), showSegmentModal = _h[0], setShowSegmentModal = _h[1];
    var profileImage = item.ProfilePhoto ? (_jsx("img", { className: cx('profile_img'), src: getProfileUrl(item), alt: "profile photo" })) : (_jsx("img", { className: cx('profile_img'), src: resultProfileImage, alt: "profile photo" }));
    var instagram_url = item.InstagramURL, whatsapp_number = item.WhatsAppNumber, facebook_url = item.facebook_url, twitter_url = item.twitter_url, youtube_url = item.youtube_url;
    var socialIconBio = useMemo(function () { return ({
        instagram_url: instagram_url,
        whatsapp_number: whatsapp_number,
        facebook_url: facebook_url,
        twitter_url: twitter_url,
        youtube_url: youtube_url,
    }); }, [facebook_url, instagram_url, twitter_url, whatsapp_number, youtube_url]);
    var showSocials = showSocialIcons && (instagram_url || whatsapp_number);
    var SocialIconProps = useMemo(function () { return getSocialIconProps(socialIconBio, socialIcons); }, [socialIconBio, socialIcons]);
    var socialIconCallback = useCallback(function (icon) {
        track('doc_loc_practice_social_click', {
            link_location: 'doc_loc_serp',
            link_text: icon === null || icon === void 0 ? void 0 : icon.label,
            link_url: icon === null || icon === void 0 ? void 0 : icon.url,
            profile_number: item.DocID,
            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) + ' Provider',
            profile_designation: getDoctorTypeText(item.DoctorType),
        });
    }, [item.DocID, item.DoctorType, item.SegmentCode]);
    var segmentToolTipHandler = function (e) {
        e.stopPropagation();
        setShowSegmentModal(true);
    };
    var customRAURL = customAppointmentUrl.find(function (it) {
        return it.docID === item.DocID;
    });
    var InfoIcon = (_jsx("svg", { width: "12", height: "12", viewBox: "0 0 20 20", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: _jsx("path", { d: "M10 0C4.48 0 0 4.48 0 10C0 15.52 4.48 20 10 20C15.52 20 20 15.52 20 10C20 4.48 15.52 0 10 0ZM11 15H9V9H11V15ZM11 7H9V5H11V7Z", fill: "#727272" }) }));
    var isHeartland = enableHeartlandButtonSwap && item.IsHeartLandOLS;
    var isHfdEnabled = document.body.classList.contains('hfdEnable') &&
        hasDynamicProgram(item, 'is_hfd_prequal') &&
        item.HfdPrequalUrl;
    var isZocdocEnabled = hasDynamicProgram(item, 'is_zocdoc') && enableZocdoc;
    var isNexHealthEnabled = hasDynamicProgram(item, 'is_nexhealth') && enableNexHealth;
    var tierData = tierPins.find(function (pins) { return pins.segmentCode === item.SegmentCode; });
    var defaultButtonSequence = [
        'zocdoc',
        'nexhealth',
        'heartland',
        'customRAUrl',
        'hfd',
        'default',
    ];
    var buttonSequence = (appointmentButtonSecquence === null || appointmentButtonSecquence === void 0 ? void 0 : appointmentButtonSecquence.length)
        ? appointmentButtonSecquence
        : defaultButtonSequence;
    var buttonConditions = {
        zocdoc: !!isZocdocEnabled,
        nexhealth: !!isNexHealthEnabled,
        heartland: !!isHeartland,
        customRAUrl: !!(customRAURL === null || customRAURL === void 0 ? void 0 : customRAURL.url),
        hfd: !!isHfdEnabled,
        default: true,
    };
    var activeButton = buttonSequence.find(function (key) { return buttonConditions[key]; }) || 'default';
    var defaultProfileUrl = "".concat(doctorProfileUrl, "?profile=").concat(item.DocID, "&requestAppointment=true").concat(vivera ? "&vivera=yes" : "").concat(programEnabled ? "&program=".concat(programEnabled) : "");
    var renderAppointmentButton = function () {
        switch (activeButton) {
            case 'zocdoc':
                return (_jsx(Button, { styleType: "primary", bgColor: "#E10098", width: "50%", 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(item.DocID),
                            profile_number: item.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(item.DoctorType),
                            is_ols: true,
                            ols_api: 'Zocdoc',
                            hfd_prequal_eligible: hasDynamicProgram(item, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(zocdocScheduleAppointmentUrl, "?id=").concat(item.DocID);
                    }, children: t.schedule_online_zocdoc_button }));
            case 'nexhealth':
                return (_jsx(Button, { styleType: "primary", bgColor: "#E10098", width: "50%", 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(item.DocID),
                            profile_number: item.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(item.DoctorType),
                            is_ols: true,
                            ols_api: 'NexHealth',
                            hfd_prequal_eligible: hasDynamicProgram(item, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(nexHealthScheduleAppointmentUrl, "?id=").concat(item.DocID);
                    }, children: t.schedule_online_nexhealth_button }));
            case 'heartland':
                return (_jsx(Button, { styleType: "primary", 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(item.DocID),
                            profile_number: item.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(item.DoctorType),
                            is_ols: item.IsHeartLandOLS,
                            ols_api: 'Heartland',
                            hfd_prequal_eligible: hasDynamicProgram(item, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(scheduleAppointmentUrl, "?id=").concat(item.DocID);
                    }, children: t.result_schedule_appointment_button }));
            case 'customRAUrl':
                return (_jsx(Button, { styleType: "primary", width: "50%", onClick: function () {
                        track('doc_loc_custom_schedule_online_click', {
                            profile_number: item.DocID,
                            link_url: customRAURL.url,
                        });
                        window.open(customRAURL.url, '_blank');
                    }, children: t.schedule_online }));
            case 'hfd':
                return (_jsx(Button, { styleType: "secondryWithIcon", 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: item === null || item === void 0 ? void 0 : item.HfdPrequalUrl,
                            profile_number: item.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(item.DoctorType),
                        });
                        window.open("".concat(t.fhd_page_url, "?id=").concat(item === null || item === void 0 ? void 0 : item.DocID, "&iframeurl=").concat((item === null || item === void 0 ? void 0 : item.HfdPrequalUrl) && encodeURIComponent(item === null || item === void 0 ? void 0 : item.HfdPrequalUrl)), '_self');
                    }, children: t.get_prequalified }));
            default:
                return (_jsx(Button, { styleType: "primary", 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: item.DocID,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: getDoctorTypeText(item.DoctorType),
                            is_ols: item.IsHeartLandOLS ||
                                hasDynamicProgram(item, 'is_zocdoc') ||
                                hasDynamicProgram(item, 'is_nexhealth'),
                            ols_api: item.IsHeartLandOLS
                                ? 'Heartland'
                                : hasDynamicProgram(item, 'is_zocdoc')
                                    ? 'Zocdoc'
                                    : hasDynamicProgram(item, 'is_nexhealth')
                                        ? 'NexHealth'
                                        : undefined,
                            hfd_prequal_eligible: hasDynamicProgram(item, 'is_hfd_prequal'),
                        });
                        window.location.href = defaultProfileUrl;
                    }, children: t.result_request_appointment_button }));
        }
    };
    return (_jsxs(_Fragment, { children: [_jsxs("div", { className: cx('doctor-card', {
                    mapView: mapView,
                    selectedDoc: mapView
                        ? isMobile && clickedMarkerId === item.DocID
                        : !isMobile && clickedMarkerId === item.DocID,
                }), onClick: function () {
                    if (!mapView) {
                        dispatch(setClickedMarkerId(item.DocID));
                    }
                }, onMouseOver: function () { return !isMobile && dispatch(setHoveredMarkerId(item.DocID)); }, onMouseLeave: function () { return !isMobile && dispatch(setHoveredMarkerId(0)); }, children: [_jsxs("div", { className: cx('content-wrap'), children: [_jsxs("div", { className: cx('profile-photo'), children: [_jsx("a", { href: "".concat(doctorProfileUrl, "?profile=").concat(item.DocID).concat(vivera ? "&vivera=yes" : "").concat(programEnabled ? "&program=".concat(programEnabled) : ""), children: profileImage }), (tierData === null || tierData === void 0 ? void 0 : tierData.imgUrl) && (_jsx("div", { className: cx('marker-place'), children: _jsx("img", { className: cx('styled-marker'), src: tierData === null || tierData === void 0 ? void 0 : tierData.imgUrl }) }))] }), _jsxs("div", { children: [getLink("".concat(doctorProfileUrl, "?profile=").concat(item.DocID).concat(vivera ? "&vivera=yes" : "").concat(programEnabled ? "&program=".concat(programEnabled) : ""), _jsx("h3", { className: "primary", children: item.PreferredName
                                            ? item.PreferredName
                                            : item.PracticeName
                                                ? item.PracticeName
                                                : item.FullName }), 'name'), _jsxs("div", { className: cx('title-wrap'), children: [_jsx("span", { className: cx('title'), children: "".concat(getDoctorTypeText(item.DoctorType)).concat((isLatam && item.LicenseNumber && " - ".concat(item.LicenseNumber)) || '') }), ' ', "| ", _jsx("span", { className: cx('textSmall'), children: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_label }), _jsx("span", { className: cx('infoIcon-small'), onClick: segmentToolTipHandler, children: InfoIcon })] }), isHfdEnabled && (_jsx("div", { className: "".concat(cx('title-wrap'), " hfd-element"), children: _jsxs("div", { style: { display: 'flex', alignItems: 'center', marginBottom: '4px' }, children: [_jsx(HfdLogo, { width: "19px" }), _jsx("span", { style: { margin: '0 0 0 4px' }, className: cx('textSmall'), children: t.hfd_tooltip_text }), _jsx("span", { className: cx('infoIcon-small'), onClick: function (e) {
                                                        e.stopPropagation();
                                                        setShowHfdModal(true);
                                                    }, children: InfoIcon })] }) })), distance && (_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, " ").concat(t.result_distance_away, " ")] })), _jsx("div", { className: cx('textSmall', useDocLoc3O && 'address_color'), children: renderAddress(item.Line1, item.Line2, item.City, item.State, item.PostalCode) })] })] }), _jsxs("div", { className: "flex", children: [showSocials ? (_jsx(SocialIconsContainer, { SocialIconProps: SocialIconProps, callback: socialIconCallback })) : (_jsx("a", { className: cx('phone-btn'), href: "tel:".concat(item.OfficePhone || item.Phone), onClick: function () {
                                    return track('doc_loc_practice_phone_click', {
                                        link_location: 'doc_loc_serp',
                                        link_text: formatPhoneNumber(item.OfficePhone || item.Phone),
                                        link_url: "tel:".concat(item.OfficePhone || item.Phone),
                                        profile_number: item.DocID,
                                        profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                                        profile_designation: getDoctorTypeText(item.DoctorType),
                                    });
                                }, children: _jsx("div", { className: cx('phone'), children: _jsx("span", { children: "".concat(formatResultPhone
                                            ? formatPhoneNumber(item.OfficePhone || item.Phone)
                                            : item.OfficePhone || item.Phone) }) }) })), renderAppointmentButton()] })] }), showSegmentModal && (_jsx(Modal, { setShowModal: function () { return setShowSegmentModal(false); }, size: "lg", title: modaldata === null || modaldata === void 0 ? void 0 : modaldata.title, description: modaldata === null || modaldata === void 0 ? void 0 : modaldata.description, closeButton: true, subTitle: _jsx("div", { className: "tier-icons-container", children: tierPins.map(function (tier) {
                        var label = tier.label, imgUrl = tier.imgUrl;
                        if (tier.label === 'VIP')
                            return _jsx(_Fragment, {});
                        return (_jsxs("div", { className: "tier-icon", children: [_jsx("img", { className: "pin", src: imgUrl }), _jsx("div", { className: "tier-label", children: label })] }, label));
                    }) }) })), showHfdModal && hfdmodaldata && (_jsx(Modal, { setShowModal: function () { return setShowHfdModal(false); }, size: (hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.size) || 'sm', title: hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.title, description: (hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.description)
                    ? hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.description.replace('{{href}}', "".concat(t.fhd_page_url, "?id=").concat(item === null || item === void 0 ? void 0 : item.DocID, "&iframeurl=").concat((item === null || item === void 0 ? void 0 : item.HfdPrequalUrl) && encodeURIComponent(item === null || item === void 0 ? void 0 : item.HfdPrequalUrl)))
                    : '', closeButton: true, children: _jsxs(_Fragment, { children: [hfdmodaldata.image && (_jsx("img", { width: '264px', style: { margin: '0px auto 24px auto' }, src: hfdmodaldata.image, alt: "" })), (hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.footnote) && (_jsx("p", { className: cx('modal_footnote'), dangerouslySetInnerHTML: {
                                __html: (_b = hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.footnote) === null || _b === void 0 ? void 0 : _b.replace('®', '<sup>®</sup>'),
                            } }))] }) })), showModal && modalData && (_jsx(ModalModule, { setShowModal: function () { return setShowModal(false); }, size: (modalData === null || modalData === void 0 ? void 0 : modalData.width) || 'sm', subTitle: modalData === null || modalData === void 0 ? void 0 : modalData.title, description: modalData === null || modalData === void 0 ? void 0 : modalData.description, closeButton: true, children: _jsxs(_Fragment, { children: [modalData.image ? (_jsx("img", { width: '264px', style: { margin: '0px auto 24px auto' }, src: modalData.image, alt: "" })) : (_jsx(_Fragment, {})), (modalData === null || modalData === void 0 ? void 0 : modalData.footnote) && (_jsx("p", { className: cx('modal_footnote'), dangerouslySetInnerHTML: {
                                __html: (_c = modalData === null || modalData === void 0 ? void 0 : modalData.footnote) === null || _c === void 0 ? void 0 : _c.replace('®', '<sup>®</sup>'),
                            } }))] }) }))] }));
});
export default DoctorCard;
