// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import React, { useEffect, useRef, useState } from 'react';
import { Link, MessageBar } from '@fluentui/react';
import { useLocale } from '../../localization';
/**
 * @private
 */
export const ComplianceBanner = (props) => {
    const strings = useLocale().strings;
    const cachedProps = useRef({
        latest: {
            callTranscribeState: false,
            callRecordState: false
        },
        previous: {
            callTranscribeState: false,
            callRecordState: false
        }
    });
    // Only update cached props if there is _some_ change in the latest props.
    // This ensures that state machine is only updated if there is an actual change in the props.
    if (props.callRecordState !== cachedProps.current.latest.callRecordState ||
        props.callTranscribeState !== cachedProps.current.latest.callTranscribeState) {
        cachedProps.current = {
            latest: props,
            previous: cachedProps.current.latest
        };
    }
    const variant = computeVariant(cachedProps.current.previous.callRecordState, cachedProps.current.previous.callTranscribeState, cachedProps.current.latest.callRecordState, cachedProps.current.latest.callTranscribeState);
    return React.createElement(DismissableMessageBar, { variant: variant, strings: strings });
};
function DismissableMessageBar(props) {
    const { variant: newVariant, strings } = props;
    const [variant, setVariant] = useState(0);
    // We drive the `MessageBar` indirectly via the `variant` state variable.
    // This allows the `onDismiss` handler to set the `variant` state to dismiss the `MessageBar`.
    // This means that when props change, this component renders *twice*: After the first render, this `useEffect` block
    // updates the value of `variant` state variable, which triggers a second render to update the message in the `MessageBar`.
    useEffect(() => {
        setVariant(newVariant);
    }, [newVariant, setVariant]);
    return variant === NO_STATE ? (React.createElement(React.Fragment, null)) : (React.createElement(MessageBar, { onDismiss: () => {
            setVariant(NO_STATE);
        }, dismissButtonAriaLabel: strings.call.close },
        React.createElement(BannerMessage, { variant: variant, strings: strings })));
}
const TRANSCRIPTION_STOPPED_STILL_RECORDING = 1;
const RECORDING_STOPPED_STILL_TRANSCRIBING = 2;
const RECORDING_AND_TRANSCRIPTION_STOPPED = 3;
const RECORDING_AND_TRANSCRIPTION_STARTED = 4;
const TRANSCRIPTION_STARTED = 5;
const RECORDING_STOPPED = 6;
const RECORDING_STARTED = 7;
const TRANSCRIPTION_STOPPED = 8;
const NO_STATE = 0;
function computeVariant(previousCallRecordState, previousCallTranscribeState, callRecordState, callTranscribeState) {
    if (previousCallRecordState && previousCallTranscribeState) {
        if (callRecordState && !callTranscribeState) {
            return TRANSCRIPTION_STOPPED_STILL_RECORDING;
        }
        else if (!callRecordState && callTranscribeState) {
            return RECORDING_STOPPED_STILL_TRANSCRIBING;
        }
        else if (!callRecordState && !callTranscribeState) {
            return RECORDING_AND_TRANSCRIPTION_STOPPED;
        }
        else {
            return NO_STATE;
        }
    }
    else if (previousCallRecordState && !previousCallTranscribeState) {
        if (callRecordState && callTranscribeState) {
            return RECORDING_AND_TRANSCRIPTION_STARTED;
        }
        else if (!callRecordState && callTranscribeState) {
            return TRANSCRIPTION_STARTED;
        }
        else if (!callRecordState && !callTranscribeState) {
            return RECORDING_STOPPED;
        }
        else {
            return NO_STATE;
        }
    }
    else if (!previousCallRecordState && previousCallTranscribeState) {
        if (callRecordState && callTranscribeState) {
            return RECORDING_AND_TRANSCRIPTION_STARTED;
        }
        else if (!callRecordState && callTranscribeState) {
            return RECORDING_STARTED;
        }
        else if (!callRecordState && !callTranscribeState) {
            return TRANSCRIPTION_STOPPED;
        }
        else {
            return NO_STATE;
        }
    }
    else if (!previousCallRecordState && !previousCallTranscribeState) {
        if (callRecordState && callTranscribeState) {
            return RECORDING_AND_TRANSCRIPTION_STARTED;
        }
        else if (callRecordState && !callTranscribeState) {
            return RECORDING_STARTED;
        }
        else if (!callRecordState && callTranscribeState) {
            return TRANSCRIPTION_STARTED;
        }
        else {
            return NO_STATE;
        }
    }
    return NO_STATE;
}
function BannerMessage(props) {
    const { variant, strings } = props;
    switch (variant) {
        case TRANSCRIPTION_STOPPED_STILL_RECORDING:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerTranscriptionStopped),
                ` ${strings.call.complianceBannerNowOnlyRecording}`,
                React.createElement(PrivacyPolicy, { linkText: strings.call.privacyPolicy })));
        case RECORDING_STOPPED_STILL_TRANSCRIBING:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerRecordingStopped),
                ` ${strings.call.complianceBannerNowOnlyTranscription}`,
                React.createElement(PrivacyPolicy, { linkText: strings.call.privacyPolicy })));
        case RECORDING_AND_TRANSCRIPTION_STOPPED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerRecordingAndTranscriptionSaved),
                ` ${strings.call.complianceBannerRecordingAndTranscriptionStopped}`,
                React.createElement(LearnMore, { linkText: strings.call.learnMore })));
        case RECORDING_AND_TRANSCRIPTION_STARTED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerRecordingAndTranscriptionStarted),
                ` ${strings.call.complianceBannerTranscriptionConsent}`,
                React.createElement(PrivacyPolicy, { linkText: strings.call.privacyPolicy })));
        case TRANSCRIPTION_STARTED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerTranscriptionStarted),
                ` ${strings.call.complianceBannerTranscriptionConsent}`,
                React.createElement(PrivacyPolicy, { linkText: strings.call.privacyPolicy })));
        case RECORDING_STOPPED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerRecordingSaving),
                ` ${strings.call.complianceBannerRecordingStopped}`,
                React.createElement(LearnMore, { linkText: strings.call.learnMore })));
        case RECORDING_STARTED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerRecordingStarted),
                ` ${strings.call.complianceBannerTranscriptionConsent}`,
                React.createElement(PrivacyPolicy, { linkText: strings.call.privacyPolicy })));
        case TRANSCRIPTION_STOPPED:
            return (React.createElement(React.Fragment, null,
                React.createElement("b", null, strings.call.complianceBannerTranscriptionSaving),
                ` ${strings.call.complianceBannerTranscriptionStopped}`,
                React.createElement(LearnMore, { linkText: strings.call.learnMore })));
    }
    return React.createElement(React.Fragment, null);
}
function PrivacyPolicy(props) {
    return (React.createElement(Link, { href: "https://privacy.microsoft.com/privacystatement#mainnoticetoendusersmodule", target: "_blank", underline: true }, props.linkText));
}
function LearnMore(props) {
    return (React.createElement(Link, { href: "https://support.microsoft.com/office/record-a-meeting-in-teams-34dfbe7f-b07d-4a27-b4c6-de62f1348c24", target: "_blank", underline: true }, props.linkText));
}
//# sourceMappingURL=ComplianceBanner.js.map