/**
 * @function stripRteStyle
 * @summary
 *  Returns string with inline style tag stripped out
 * @param {string} stringWithStyleTag
 * @returns {string}
 */
export function stripRteStyle(stringWithStyleTag) {
    const regexExp = /[\s*]style="(.*?)"/g;
    return stringWithStyleTag.replace(regexExp, '');
}

export function extractSpanText(spanElement) {
    const tempContainer = document.createElement('div');
    tempContainer.innerHTML = spanElement;
    return tempContainer.textContent || tempContainer.innerText || '';
}

/**
 * @function createOrderedListFromStringOfParagraphs
 * @summary
 *  Returns string with <p> tags changed to <li> and wrapped in an <ol>
 * @param {string} stringOfParagraphs single string with 1 to N <p> tags
 * @returns {string} converted string wrapped in an <ol>
 */
export function createOrderedListFromStringOfParagraphs(stringOfParagraphs) {
    return `<ol>${stringOfParagraphs.replaceAll('<p></p>', '').replaceAll('<p>', '<li>').replaceAll('</p>', '</li>')}</ol>`;
}

/**
 * @function createUnorderedListFromStringOfParagraphs
 * @summary
 *  Returns string with <p> tags changed to <li> and wrapped in an <ul>
 * @param {string} stringOfParagraphs single string with 1 to N <p> tags
 * @returns {string} converted string wrapped in an <ul>
 */
export function createUnorderedListFromStringOfParagraphs(stringOfParagraphs) {
    return `<ul>${stringOfParagraphs.replaceAll('<p></p>', '').replaceAll('<p>', '<li>').replaceAll('</p>', '</li>')}</ul>`;
}

/**
 * @function toTitleCase
 * @summary Returns title cased string version of 'text'
 * @param {string} text
 * @returns {string}
 */
export function toTitleCase(text) {
    let temp = text?.toLowerCase() ?? '';
    return temp.replace(/(^|\s|-)\S/g, (letter) => letter.toUpperCase());
}

/**
 * @function toDisplayPhoneNumber
 * @summary If 10 or 11 digits are in phoneNumber, then the expected number including dashes will be returned
 * in either d-ddd-ddd-dddd or ddd-ddd-dddd format. Otherwise, an empty string will be returned.
 * @param {string} phoneNumber
 * @returns {string}
 */
export function toDisplayPhoneNumber(phoneNumber) {
    let result = '';
    if (!phoneNumber) { return result; }
    let remainingDigits = phoneNumber.match(/\d+/g).join('');
    if (remainingDigits.length === 11) {
        result += `${remainingDigits[0]}-`;
        remainingDigits = remainingDigits.substring(1);
    }
    if (remainingDigits.length === 10) {
        result += remainingDigits.replace(/^(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
    }
    return result;
}

/**
 * @function formatAddress
 * @summary given address fields, returns a string representation of said address
 * @param {string} addressLine1
 * @param {string} addressLine2
 * @param {string} city
 * @param {string} state
 * @param {string} zipCode
 * @returns {string}
 */
export function formatAddress(addressLine1, addressLine2, city, state, zipCode) {
    let address = '';

    if (addressLine1) {
        address += addressLine1;
    }

    if (addressLine2) {
        address += address ? `, ${addressLine2}` : addressLine2;
    }

    if (city) {
        address += address ? `, ${city}` : city;
    }
    address = toTitleCase(address);

    if (state) {
        address += address ? `, ${state}` : state;
    }

    if (zipCode) {
        address += address ? ` ${zipCode}` : zipCode;
    }

    return address;
}

const currencyFormatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD'
});

/**
 * @function formatAmountInDollars
 * @param {string, number} amount
 * @returns {string}
 */
export function formatAmountInDollars(amount) {
    const numericAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
    if (Number.isNaN(numericAmount) || amount === null || amount === undefined) {
        return '';
    }
    return currencyFormatter.format(amount);
}

const defaultDateFormatter = new Intl.DateTimeFormat('en-US');
export function formatDate(date, formatter) {
    const dateFormatter = formatter ?? defaultDateFormatter;
    if (!Number.isNaN(date)) {
        return dateFormatter.format(date);
    }
    return '';
}
