import i18next from 'i18next';
import { globals } from './Globals.svelte';
// Triggers on the next change, ignoring the initial value
export function onNextChange(store, onChange) {
    let first = true;
    const unsub = store.subscribe(value => {
        if (first)
            return (first = false);
        unsub();
        onChange(value);
    });
}
/**
 * HTML escapes content. For use with other HTML-adding formatters such as autolink.
 */
export function escape(value) {
    const div = document.createElement('div');
    div.textContent = value || '';
    return div.innerHTML;
}
export function unescape(value) {
    const div = document.createElement('div');
    div.innerHTML = value;
    return div.textContent;
}
/**
 * Adds <br> tags in place of newlines characters.
 */
export function br(value) {
    const lines = (value || '').split(/\r?\n/);
    return lines.join('<br>\n');
}
export function unbr(value) {
    return value.replace(/<br>\r?\n?/g, '\n');
}
const urlExp = /(^|\s|\()((https?:\/\/|www\.)([-A-Z0-9]+)+\.\w{2,}[-A-Z0-9+\u0026@#/%?=()~_|!:,.;]*[-A-Z0-9+\u0026@#/%=~(_|])/gi;
const localExp = /^\s*(https?:\/\/)?(localhost|127\.0\.0\.1)/gi;
const tagOrContentsExp = /<[^>]+>|[^<]+/g; // Use this to skip over tags by selecting them
/**
 * Adds automatic links to escaped content (be sure to escape user content). Can be used on existing HTML content as it
 * will skip URLs within HTML tags. Passing a value in the second parameter will set the target to that value or
 * `_blank` if the value is `true`.
 */
export function autolink(value, target) {
    let targetString = '';
    if (typeof target === 'string') {
        targetString = ' target="' + target + '"';
    }
    else if (target) {
        targetString = ' target="_blank"';
    }
    return ('' + value).replace(tagOrContentsExp, match => {
        // short-circuit if it's a localhost URL or in a tag
        if (match.charAt(0) === '<' || localExp.test(match)) {
            return match;
        }
        return match.replace(urlExp, function (_, char, url, start) {
            return char + '<a href="' + (start === 'www.' ? 'https://' : '') + url + '"' + targetString + '>' + url + '</a>';
        });
    });
}
export function number(value, decimalPoints = 0) {
    if (typeof value !== 'number')
        return undefined;
    return new Intl.NumberFormat(i18next.language, {
        minimumFractionDigits: decimalPoints,
        maximumFractionDigits: decimalPoints,
    }).format(value);
}
export function parseInteger(value) {
    if (!value)
        return undefined;
    return parseInt(value.replace(/\D/, ''));
}
export function price(value, currency = 'USD') {
    return new Intl.NumberFormat(i18next.language, { style: 'currency', currency }).format(value);
}
export function percent(value) {
    return new Intl.NumberFormat(i18next.language, { style: 'percent' }).format(value);
}
export function confirm(title, message, options) {
    return globals.confirm(title, message, options);
}
export function prompt(title, message, options) {
    return globals.prompt(title, message, options);
}
