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, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useState } from 'react';
import { useAppSelector } from 'src/app/redux/store';
import { DOC_TYPE_NAME } from 'src/lib/constants';
import { DocLocViews } from 'src/lib/types/types';
import { renderAddress } from 'src/pages/ResultPage/ResultPage';
import Modal from 'src/components/Modal/DetailModal/ModalModule';
import ModalModule from 'src/components/Modal/ModalModule';
import { getDistanceByLatLng, getDoctorTypeText, formatPhoneNumber, getWebsiteLink, hasProgram, } from 'src/lib/common/helpers';
import Button from 'src/components/Button';
import Advantage from './Advantage';
import DSOProfile from '../DSODoctorProfile';
import { track } from 'src/lib/common/helpers/tracking';
import css from './DoctorProfile.module.scss';
import { _cx } from 'src/utility/class';
import { getSocialIconProps, SocialIconsContainer } from './lib';
import { HfdLogo } from 'src/lib/utils';
var cx = _cx(css);
export default function Profile(_a) {
    var _b, _c, _d, _e, _f, _g, _h;
    var data = _a.data, profileId = _a.profileId, t = _a.t, isMobile = _a.isMobile, view = _a.view, setDsoId = _a.setDsoId, showAppointmentModal = _a.showAppointmentModal, _j = _a.showAddress, showAddress = _j === void 0 ? false : _j, _k = _a.hideAction, hideAction = _k === void 0 ? false : _k, _l = _a.hideDistance, hideDistance = _l === void 0 ? false : _l, _m = _a.isModal, isModal = _m === void 0 ? false : _m, _o = _a.hideWebsite, hideWebsite = _o === void 0 ? false : _o, closeModal = _a.closeModal;
    var _p = __read(useState(false), 2), showHfdModal = _p[0], setShowHfdModal = _p[1];
    var _q = __read(useState(false), 2), showModal = _q[0], setShowModal = _q[1];
    var _r = __read(useState(), 2), modalData = _r[0], setModalData = _r[1];
    var _s = useAppSelector(function (state) { return state.config.config; }), country = _s.country, countryData = _s.countryData, doctorProfileImage = _s.doctorProfileImage, isLatam = _s.isLatam, customAppointmentUrl = _s.customAppointmentUrl, enableHeartlandButtonSwap = _s.enableHeartlandButtonSwap, scheduleAppointmentUrl = _s.scheduleAppointmentUrl, zocdocScheduleAppointmentUrl = _s.zocdocScheduleAppointmentUrl, useDocLoc3O = _s.useDocLoc3O, socialIcons = _s.socialIcons, showSocialIcons = _s.showSocialIcons, tierPins = _s.tierPins, modalsData = _s.modalsData, enableZocdoc = _s.enableZocdoc, enableNexHealth = _s.enableNexHealth, nexHealthScheduleAppointmentUrl = _s.nexHealthScheduleAppointmentUrl, appointmentButtonSecquence = _s.appointmentButtonSecquence;
    var programEnabled = useAppSelector(function (state) { return state.common.programEnabled; });
    var hfdmodaldata = modalsData.find(function (item) { return item.id === 'hfd_financing_modal'; });
    var zipLatLng = useAppSelector(function (state) { return state.common.zipLatLng; });
    var countryDataByCountry = countryData[country];
    var apiDistUnit = countryDataByCountry === null || countryDataByCountry === void 0 ? void 0 : countryDataByCountry.apiDistUnit;
    var distUnit = (_b = countryDataByCountry === null || countryDataByCountry === void 0 ? void 0 : countryDataByCountry.distUnit) !== null && _b !== void 0 ? _b : 'mile';
    var distance = useMemo(function () {
        return getDistanceByLatLng(zipLatLng.lat, data.latitude, zipLatLng.lng, data.longitude, apiDistUnit || distUnit);
    }, [apiDistUnit, data.latitude, data.longitude, distUnit, zipLatLng.lat, zipLatLng.lng]);
    var tierData = tierPins.find(function (pins) { return pins.segmentCode === data.advantage_level; });
    var showButton = useMemo(function () { return (data.bio && Object.keys(data.bio).length > 0) || view === DocLocViews.DOC_PROFILE; }, [data, view]);
    var hasBio = data.bio &&
        !!Object.keys(data.bio).length &&
        ((data.bio.instagram_url != null && data.bio.instagram_url !== '') ||
            (data.bio.whatsapp_number != null && data.bio.whatsapp_number !== '') ||
            (data.bio.facebook_url != null && data.bio.facebook_url !== '') ||
            (data.bio.twitter_url != null && data.bio.twitter_url !== '') ||
            (data.bio.youtube_url != null && data.bio.youtube_url !== ''));
    var showSocials = showSocialIcons && hasBio;
    var websiteLink = useMemo(function () { return getWebsiteLink(data.url); }, [data]);
    var _t = (data === null || data === void 0 ? void 0 : data.bio) || {}, instagram_url = _t.instagram_url, whatsapp_number = _t.whatsapp_number, facebook_url = _t.facebook_url, twitter_url = _t.twitter_url, youtube_url = _t.youtube_url;
    var socialIconsBio = 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 SocialIconProps = useMemo(function () { return getSocialIconProps(socialIconsBio, socialIcons); }, [socialIcons, socialIconsBio]);
    var socialIconCallback = function (icon) {
        track('doc_loc_practice_social_click', {
            link_location: 'doc_loc_profile',
            link_text: icon === null || icon === void 0 ? void 0 : icon.label,
            link_url: icon === null || icon === void 0 ? void 0 : icon.url,
            profile_number: profileId,
            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) + ' Provider',
            profile_designation: data.doctor_type === 'Ortho' ? 'Orthodontist' : 'General Dentist',
        });
    };
    var updateModalInfo = function (modal_id) {
        var modaldata = modalsData.find(function (item) { return item.id === modal_id; });
        setModalData(modaldata);
    };
    var isHeartland = enableHeartlandButtonSwap && data.is_heartland_ols;
    var customRAURL = customAppointmentUrl.find(function (it) {
        return it.docID === data.location_id;
    });
    var isHfdEnabled = document.body.classList.contains('hfdEnable') &&
        hasProgram(data, 'is_hfd_prequal') &&
        (data === null || data === void 0 ? void 0 : data.hfd_prequal_url);
    var isZocdocEnabled = hasProgram(data, 'is_zocdoc') && enableZocdoc;
    var isNexHealthEnabled = hasProgram(data, '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 = {
        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 renderAppointmentButton = function () {
        switch (activeButton) {
            case 'zocdoc':
                return (_jsx(Button, { styleType: "primary", bgColor: "#E10098", width: "50%", textWeight: 500, fontSize: "14px", padding: "12px", margin: "0", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_profile',
                            link_text: t.schedule_online_zocdoc_button,
                            link_url: "".concat(zocdocScheduleAppointmentUrl, "?id=").concat(data.location_id),
                            profile_number: profileId,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                            is_ols: true,
                            ols_api: 'Zocdoc',
                            hfd_prequal_eligible: hasProgram(data, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(zocdocScheduleAppointmentUrl, "?id=").concat(data.location_id);
                    }, children: t.schedule_online_zocdoc_button }));
            case 'nexhealth':
                return (_jsx(Button, { styleType: "primary", bgColor: "#E10098", width: "50%", textWeight: 500, fontSize: "14px", padding: "12px", margin: "0", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_profile',
                            link_text: t.schedule_online_nexhealth_button,
                            link_url: "".concat(nexHealthScheduleAppointmentUrl, "?id=").concat(data.location_id),
                            profile_number: profileId,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                            is_ols: true,
                            ols_api: 'NexHealth',
                            hfd_prequal_eligible: hasProgram(data, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(nexHealthScheduleAppointmentUrl, "?id=").concat(data.location_id);
                    }, children: t.schedule_online_nexhealth_button }));
            case 'heartland':
                return (_jsx(Button, { styleType: "normal", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_profile',
                            link_text: t.result_schedule_appointment_button,
                            link_url: "".concat(scheduleAppointmentUrl, "?id=").concat(data.location_id),
                            profile_number: profileId,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                            is_ols: !!data.is_heartland_ols,
                            ols_api: 'Heartland',
                            hfd_prequal_eligible: hasProgram(data, 'is_hfd_prequal'),
                        });
                        window.location.href = "".concat(scheduleAppointmentUrl, "?id=").concat(data.location_id);
                    }, textWeight: 500, fontSize: "14px", padding: "12px", margin: "0", width: "50%", children: t.result_schedule_appointment_button }));
            case 'customRAUrl':
                return (_jsx(Button, { styleType: "normal", onClick: function () {
                        track('doc_loc_custom_schedule_online_click', {
                            profile_number: data.location_id,
                            link_url: customRAURL.url,
                        });
                        window.open(customRAURL.url, '_blank');
                    }, textWeight: 500, fontSize: "14px", padding: "12px", margin: "0", width: "50%", 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_profile',
                            link_text: t.get_prequalified,
                            link_url: data === null || data === void 0 ? void 0 : data.hfd_prequal_url,
                            profile_number: data.location_id,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                        });
                        window.open("".concat(t.fhd_page_url, "?id=").concat(data === null || data === void 0 ? void 0 : data.location_id, "&iframeurl=").concat((data === null || data === void 0 ? void 0 : data.hfd_prequal_url) && encodeURIComponent(data === null || data === void 0 ? void 0 : data.hfd_prequal_url)), '_self');
                    }, children: t.get_prequalified }));
            default:
                return (_jsx(Button, { styleType: "normal", onClick: function () {
                        track('doc_loc_form_cta_click', {
                            link_location: 'doc_loc_profile',
                            link_text: t.result_request_appointment_button,
                            link_url: 'showAppointmentModal',
                            profile_number: profileId,
                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                            is_ols: !!data.is_heartland_ols ||
                                hasProgram(data, 'is_zocdoc') ||
                                hasProgram(data, 'is_nexhealth'),
                            ols_api: data.is_heartland_ols
                                ? 'Heartland'
                                : hasProgram(data, 'is_zocdoc')
                                    ? 'Zocdoc'
                                    : hasProgram(data, 'is_nexhealth')
                                        ? 'NexHealth'
                                        : undefined,
                            hfd_prequal_eligible: hasProgram(data, 'is_hfd_prequal'),
                        });
                        showAppointmentModal && showAppointmentModal();
                    }, textWeight: 500, fontSize: "14px", padding: "12px", margin: "0", width: "50%", children: t.result_request_appointment_button }));
        }
    };
    return (_jsxs(_Fragment, { children: [_jsxs("div", { className: cx('profile-container content-wrap', {
                    isMobile: isMobile,
                    dsoEnable: data.is_dso && !!data.dso_practice_info.length,
                }), style: isModal ? { marginBottom: '20px' } : {}, children: [_jsx("div", { className: cx('profile-photo-wrap'), children: _jsxs(_Fragment, { children: [_jsx("div", { className: cx('profile-photo'), style: {
                                        backgroundImage: useDocLoc3O
                                            ? "url(".concat(((_c = data.bio) === null || _c === void 0 ? void 0 : _c.photo) || doctorProfileImage, ")")
                                            : "url(".concat(((_f = (_e = (_d = data.bio) === null || _d === void 0 ? void 0 : _d.photo) === null || _e === void 0 ? void 0 : _e.filename) === null || _f === void 0 ? void 0 : _f.url) || doctorProfileImage, ")"),
                                    } }), (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", { className: cx('doc-details-wrap'), children: [data.is_dso || (data.is_dso && !!data.dso_practice_info.length) ? (_jsx("h1", { className: cx('doc-name', { small: isModal }), children: data.practice_name ||
                                    (data === null || data === void 0 ? void 0 : data.practice_name2) ||
                                    "".concat(data.first_name || '', " ").concat(data.last_name || '') })) : (((data === null || data === void 0 ? void 0 : data.preferred_name) ||
                                (data === null || data === void 0 ? void 0 : data.display_name) ||
                                (data === null || data === void 0 ? void 0 : data.FullName) ||
                                (data === null || data === void 0 ? void 0 : data.first_name) ||
                                (data === null || data === void 0 ? void 0 : data.last_name)) && (_jsx("h1", { className: cx('doc-name', { small: isModal }), children: data.preferred_name ||
                                    data.display_name ||
                                    data.FullName ||
                                    "".concat(data.first_name || '', " ").concat(data.last_name || '') }))), _jsxs("div", { className: cx('profile-details'), style: { marginBottom: isModal ? '5px' : '10px' }, children: [data.doctor_type && (_jsxs("span", { children: [getDoctorTypeText(data.doctor_type), isLatam && (data === null || data === void 0 ? void 0 : data.license_number) && " - ".concat(data.license_number)] })), data.advantage_level && (_jsxs(_Fragment, { children: ["\u00A0", '|', "\u00A0", _jsx(Advantage, { t: t, data: data })] }))] }), isHfdEnabled && (_jsx("div", { className: "".concat(cx('profile-details'), " 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: _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" }) }) })] }) })), !hideDistance && zipLatLng.lat && (_jsx("span", { className: cx('distance'), children: "".concat(t.doctor_distance_prefix || '', " ").concat(distance, " ").concat(t.doctor_distance_postfix).trim() })), showAddress && (_jsx("div", { className: cx('address'), children: renderAddress(data.line1, data.line2, data.city, data.state, data.postal_code) })), !hideWebsite && (_jsx("div", { className: cx('website-link'), children: data.url && (_jsx("a", { className: cx('contact-link'), href: "http://".concat(websiteLink), rel: "noreferrer noopener", target: "_blank", onClick: function () {
                                        return track('doc_loc_practice_website_click', {
                                            link_location: 'doc_loc_profile',
                                            link_text: data.url,
                                            link_url: "http://".concat(websiteLink),
                                            profile_number: profileId,
                                            profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                                            profile_designation: DOC_TYPE_NAME[data.doctor_type],
                                        });
                                    }, children: websiteLink })) })), isModal && t.view_profile_btn && (_jsx("a", { onClick: function () { return closeModal === null || closeModal === void 0 ? void 0 : closeModal(); }, className: cx('view-profile-btn'), children: t.view_profile_btn })), !hideAction && !isMobile && (_jsxs("div", { children: [!showSocials && data.phone && (_jsx("a", { className: cx('phone-btn'), href: "tel:".concat(data.phone), onClick: function () {
                                            return track('doc_loc_practice_phone_click', {
                                                link_location: 'doc_loc_profile',
                                                link_text: formatPhoneNumber(data.phone),
                                                link_url: "tel:".concat(data.phone),
                                                profile_number: profileId,
                                                profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                                                profile_designation: DOC_TYPE_NAME[data.doctor_type],
                                            });
                                        }, children: formatPhoneNumber(data.phone) })), showSocials && (_jsx(SocialIconsContainer, { SocialIconProps: SocialIconProps, callback: socialIconCallback, style: { justifyContent: 'unset' } }))] }))] })] }), !hideAction && isMobile && (_jsxs("div", { className: "profile-mobile-action-selector ".concat(cx('mobile-action-btn flex', {
                    dsoEnable: data.is_dso && !!data.dso_practice_info.length,
                    justifyUnset: showSocials,
                })), "data-is-heartland-ols": data.is_heartland_ols, "data-is-hfd-prequal": hasProgram(data, 'is_hfd_prequal'), "data-doc-id": data.location_id, children: [!showSocials && data.phone && (_jsx("a", { className: cx('phone-btn'), href: "tel:".concat(data.phone), onClick: function () {
                            return track('doc_loc_practice_phone_click', {
                                link_location: 'doc_loc_profile',
                                link_text: formatPhoneNumber(data.phone),
                                link_url: "tel:".concat(data.phone),
                                profile_number: profileId,
                                profile_tier: (tierData === null || tierData === void 0 ? void 0 : tierData.label) || t.no_tier_event_label,
                                profile_designation: DOC_TYPE_NAME[data.doctor_type],
                            });
                        }, children: formatPhoneNumber(data.phone) })), showSocials && (_jsx(SocialIconsContainer, { SocialIconProps: SocialIconProps, callback: socialIconCallback })), showButton && renderAppointmentButton()] })), data.is_dso && !!data.dso_practice_info.length && setDsoId && (_jsx(DSOProfile, { t: t, data: data, setDsoId: setDsoId, isMobile: isMobile })), isModal && _jsx("hr", { style: { marginTop: '15px' } }), 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(data === null || data === void 0 ? void 0 : data.location_id, "&iframeurl=").concat((data === null || data === void 0 ? void 0 : data.hfd_prequal_url) && encodeURIComponent(data === null || data === void 0 ? void 0 : data.hfd_prequal_url)))
                    : '', 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: (_g = hfdmodaldata === null || hfdmodaldata === void 0 ? void 0 : hfdmodaldata.footnote) === null || _g === void 0 ? void 0 : _g.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: (_h = modalData === null || modalData === void 0 ? void 0 : modalData.footnote) === null || _h === void 0 ? void 0 : _h.replace('®', '<sup>®</sup>'),
                            } }))] }) }))] }));
}
