{"size": 3030, "ext": "js", "max_stars_repo_path": "packages/time-grid/src/stores.js", "max_stars_repo_name": "vkurko/calendar", "max_stars_repo_stars_event_min_datetime": "2020-11-18T11:25:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T07:35:13.000Z", "max_issues_repo_path": "packages/time-grid/src/stores.js", "max_issues_repo_name": "code-wisdoms/calendar", "max_issues_repo_issues_event_min_datetime": "2020-12-29T03:46:30.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T22:27:01.000Z", "max_forks_repo_path": "packages/time-grid/src/stores.js", "max_forks_repo_name": "code-wisdoms/calendar", "max_forks_repo_forks_event_min_datetime": "2021-08-18T11:50:47.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-14T11:12:16.000Z"}
{"max_stars_count": 72, "max_issues_count": 35, "max_forks_count": 4, "avg_line_length": 42.0833333333, "max_line_length": 110, "alphanum_fraction": 0.4788778878}
import {derived} from 'svelte/store';
import {
    DAY_IN_SECONDS,
    createDate,
    cloneDate,
    addDuration,
    createDuration
} from '@event-calendar/common';

export function times(state, localState) {
    return derived(
        [localState._slotTimeLimits, state._intlSlotLabel, state.slotDuration],
        ([$_slotTimeLimits, $_intlSlotLabel, $slotDuration]) => {
            let compact = $slotDuration.seconds >= 3600;
            let times = [];
            let date = createDate('2020-01-01');
            let end = cloneDate(date);
            let i = 1;
            addDuration(date, $_slotTimeLimits.min);
            addDuration(end, $_slotTimeLimits.max);
            while (date < end) {
                times.push(times.length && (i || compact) ? $_intlSlotLabel.format(date) : '');
                addDuration(date, $slotDuration);
                i = 1 - i;
            }

            return times;
        }
    );
}

export function slotTimeLimits(state) {
    return derived(
        [state._events, state._viewDates, state.flexibleSlotTimeLimits, state.slotMinTime, state.slotMaxTime],
        ([$_events, $_viewDates, $flexibleSlotTimeLimits, $slotMinTime, $slotMaxTime]) => {
            let min = createDuration($slotMinTime);
            let max = createDuration($slotMaxTime);

            if ($flexibleSlotTimeLimits) {
                let minMin = createDuration(Math.min(min.seconds, Math.max(0, max.seconds - DAY_IN_SECONDS)));
                let maxMax = createDuration(Math.max(max.seconds, minMin.seconds + DAY_IN_SECONDS));
                loop: for (let date of $_viewDates) {
                    let start = addDuration(cloneDate(date), min);
                    let end = addDuration(cloneDate(date), max);
                    let minStart = addDuration(cloneDate(date), minMin);
                    let maxEnd = addDuration(cloneDate(date), maxMax);
                    for (let event of $_events) {
                        if (event.display === 'auto' && event.start < maxEnd && event.end > minStart) {
                            if (event.start < start) {
                                let seconds = Math.max((event.start - date) / 1000, minMin.seconds);
                                if (seconds < min.seconds) {
                                    min.seconds = seconds;
                                }
                            }
                            if (event.end > end) {
                                let seconds = Math.min((event.end - date) / 1000, maxMax.seconds);
                                if (seconds > max.seconds) {
                                    max.seconds = seconds;
                                }
                            }
                            if (min.seconds === minMin.seconds && max.seconds === maxMax.seconds) {
                                break loop;
                            }
                        }
                    }
                }
            }

            return {min, max};
        }
    );
}