import { DistanceUnit } from '@brandingbrand/fsfoundation';
export function formatHours(hours, currentDate, format) {
    const date = currentDate || new Date();
    const hourOfDay = hours.find(h => h.dayOfWeek === date.getDay());
    if (!hourOfDay)
        return ``;
    switch (format) {
        case '1':
            return `Open ${hourOfDay.open} to ${hourOfDay.close}`;
        case '2':
            return `Open until ${hourOfDay.close}`;
        case '3':
            const isOpen = checkIfOpen(date, hourOfDay.open, hourOfDay.close);
            return `${isOpen ? 'Open' : 'Closed'} | Closes ${hourOfDay.close}`;
        default:
            return `Open ${hourOfDay.open} to ${hourOfDay.close}`;
    }
}
function checkIfOpen(currentDate, open, close) {
    const _open = normalizedHourString(open);
    const _close = normalizedHourString(close);
    const _current = currentDate.getHours() * 100 + currentDate.getMinutes();
    return _current >= _open && _current <= _close;
}
function normalizedHourString(hour) {
    const isAM = hour.indexOf('AM') > -1;
    let hourNumber = 0;
    if (hour.indexOf(':') > -1) {
        const split = hour.match(/(\d+):(\d+)/);
        if (split && split.length > 2) {
            hourNumber = +split[1] * 100 + +split[2];
        }
    }
    else {
        const split = hour.match(/(\d+)/);
        if (split && split.length > 1) {
            hourNumber = +split[1] * 100;
        }
    }
    if (!isAM) {
        hourNumber += 1200;
    }
    return hourNumber;
}
export function formatDistance(distance, format) {
    let unit = 'mi';
    if (distance.unit === DistanceUnit.Mile) {
        if (format === 'full') {
            if (distance.value === 1) {
                unit = 'mile';
            }
            else {
                unit = 'miles';
            }
        }
        else {
            unit = 'mi';
        }
    }
    else {
        if (format === 'full') {
            if (distance.value === 1) {
                unit = 'kilometer';
            }
            else {
                unit = 'kilometers';
            }
        }
        else {
            unit = 'km';
        }
    }
    return `${distance.value} ${unit}`;
}
export function formatAddress(address) {
    return address.address1;
}
export function animatedScrollTo(element, target, duration) {
    target = Math.round(target);
    duration = Math.round(duration);
    if (duration < 0) {
        return Promise.reject('bad duration');
    }
    if (duration === 0) {
        element.scrollLeft = target;
        return Promise.resolve();
    }
    let start_time = Date.now();
    let end_time = start_time + duration;
    let start_top = element.scrollLeft;
    let distance = target - start_top;
    let smooth_step = function (start, end, point) {
        if (point <= start) {
            return 0;
        }
        if (point >= end) {
            return 1;
        }
        let x = (point - start) / (end - start);
        return x * x * (3 - 2 * x);
    };
    return new Promise(function (resolve, reject) {
        let previous_top = element.scrollLeft;
        let scroll_frame = function () {
            if (element.scrollLeft != previous_top) {
                return;
            }
            let now = Date.now();
            let point = smooth_step(start_time, end_time, now);
            let frameTop = Math.round(start_top + distance * point);
            element.scrollLeft = frameTop;
            if (now >= end_time) {
                resolve();
                return;
            }
            if (element.scrollLeft === previous_top &&
                element.scrollLeft !== frameTop) {
                resolve();
                return;
            }
            previous_top = element.scrollLeft;
            requestAnimationFrame(scroll_frame);
        };
        requestAnimationFrame(scroll_frame);
    });
}
//# sourceMappingURL=helpers.js.map