<template>
    <div>
        <div
            class="subheader-primary d-flex overflow-hidden">
            <h5
                :class="[justifySubheader, headerColor]"
                class="fw-normal mb-0 subheader-primary">
                <span>
                    {{ content }}
                </span>
                <buttonBack
                    v-if="hasBackButton"
                    :backButtonAccessibleText="backButtonAccessibleText"
                    @clickEvent="clickEvent" />
            </h5>
        </div>
        <div
            v-if="hasSecondaryText"
            :class="[justifySubheaderSecondaryText, subHeaderClasses, subHeaderMarginClasses]"
            class="subheader-secondary d-flex align-items-start overflow-hidden">
            <p
                class="dark-header"
                :class="[alternateFormatting, secondaryTextClasses]">
                <span v-html="secondaryText"> </span>
            </p>
        </div>
    </div>
</template>

<script>
import buttonBack from '@/iss-components/site-sub-header/button-back/button-back.vue';
import {
    doesCopyContainRouterLink,
    splitCopyOnCMSPlaceHolder,
    getRouterLinkRouteFromCopy,
    getRouterLinkDisplayTextFromCopy,
    getRouterLinkHtmlStringFromCopy
} from '@/helpers/cms-content-helper';

import { stripRteStyle } from '@/helpers/text-helper';

export default {
    name: 'site-sub-header',
    components: { buttonBack },
    props: {
        cmsWidgetName: String,
        contentProperty: String,
        darkGraySubText: Boolean,
        hasBackButton: Boolean,
        issContainingPage: String,
        justification: String,
        justificationSecondaryText: String,
        stripRteStyle: Boolean,
        subContentProperty: String,
        subHeaderClasses: String,
        subHeaderMarginClasses: {
            type: String,
            default: 'mt-1'
        },
        secondaryTextClasses: String
    },
    emits: ['click-event'],
    computed: {
        content() {
            return this.getCmsContent(
                this.cmsWidgetName,
                this.contentProperty ?? 'SubHeaderText'
            );
        },
        secondaryText() {
            let secondaryTextFromCms = this.getCmsContent(
                this.cmsWidgetName,
                this.subContentProperty ?? 'SecondaryText'
            );

            if (this.stripRteStyle) {
                secondaryTextFromCms = stripRteStyle(secondaryTextFromCms);
            }

            let secondaryText = '';
            if (this.doesCopyContainRouterLink(secondaryTextFromCms)) {
                splitCopyOnCMSPlaceHolder(secondaryTextFromCms).forEach((sc) => {
                    if (this.doesCopyContainRouterLink(sc)) {
                        secondaryText += getRouterLinkHtmlStringFromCopy(sc);
                    } else {
                        secondaryText += sc;
                    }
                });
            } else {
                secondaryText = secondaryTextFromCms;
            }

            return secondaryText ?? '';
        },
        hasSecondaryText() {
            return this.secondaryText.length > 0;
        },
        backButtonAccessibleText() {
            return this.getCmsContent(
                this.cmsWidgetName,
                'BackButtonAccessibleText'
            );
        },
        headerColor() {
            return this.secondaryText ? 'dark-header' : 'light-header';
        },
        justifySubheader() {
            return this.justification?.toLowerCase() === 'center'
                ? 'justify-content-center'
                : 'justify-content-left';
        },
        justifySubheaderSecondaryText() {
            return this.justificationSecondaryText?.toLowerCase() === 'center'
                ? 'justify-content-center'
                : 'justify-content-left';
        },
        alternateFormatting() {
            // override for service-packages unique style
            if (this.issContainingPage?.toLowerCase() === 'service-packages') {
                return 'service-packages-subtext my-4';
            }
            return this.darkGraySubText ? 'dark-gray' : 'light-gray';
        }
    },
    methods: {
        doesCopyContainRouterLink,
        splitCopyOnCMSPlaceHolder,
        getRouterLinkRouteFromCopy,
        getRouterLinkDisplayTextFromCopy,
        getRouterLinkHtmlStringFromCopy,
        clickEvent() {
            this.$emit('click-event');
        }
    }
};
</script>

<style lang="scss" scoped>
.dark-header {
    color: $black;
}

.light-header {
    color: $gray-600;
}

h5 {
    line-height: 32px;

    button {
        color: inherit;
    }
    span {
        color: $black;
    }
    &.justify-content-center {
        text-align: center;
    }
    &.justify-content-left {
        text-align: left;
    }
}
p {
    margin: 0;
    &.light-gray {
        color: $gray-600;
    }
    &.dark-gray {
        color: $darker-gray;
    }
    &.service-packages-subtext {
        font-weight: 500;
        line-height: 24px;
        span {
            font-size: 1rem;
        }
    }
}

.subheader-secondary {
    p {
        margin-bottom: 1.25rem;
        color: #525656;
        font-size: 1rem;
        line-height: 1.56;
        &.justify-content-center {
            text-align: center;
        }
        &.justify-content-left {
            text-align: left;
        }
    }
}
</style>
