import { PresetSelect as e } from "../preset-select/index.js";
import { useEffect as t, useEffectEvent as n, useMemo as r, useRef as i } from "react";
import { jsx as a } from "react/jsx-runtime";
import { countCalendarDaysInDateRange as o, normalizeText as s, toPositiveInteger as c } from "@ryuzaki13/react-foundation-lib/formatters";
import { getPresetOption as l, normalizePresetIds as u, resolvePresetOptionsByIds as d } from "@ryuzaki13/react-foundation-lib/presets";
import { isRecord as f } from "@ryuzaki13/react-foundation-lib/validators";
//#region src/period-select/lib/constants.ts
var p = "period-select", m = "day", h = 31, g = 26, _ = Object.freeze([
	{
		id: "day",
		label: "по дням"
	},
	{
		id: "week",
		label: "по неделям"
	},
	{
		id: "month",
		label: "по месяцам"
	},
	{
		id: "year",
		label: "по годам"
	}
]), v = Object.freeze([
	"day",
	"week",
	"month",
	"year"
]), y = new Set(_.map((e) => e.id));
function b(e) {
	return typeof e == "string" && y.has(e);
}
function x(e, t = v) {
	let n = u(e, b, t), r = n.length > 0 ? n : v, i = new Set(r);
	return v.filter((e) => i.has(e));
}
function S(e, t = _) {
	return d(e, t);
}
function C(e, t = _) {
	return l(e, t);
}
function w(e) {
	return b(e) ? e : "day";
}
function T({ maxDayRangeDays: e, maxWeekRangeWeeks: t } = {}) {
	let n = c(e), r = c(t);
	return _.map((e) => e.id === "day" && n !== void 0 ? {
		...e,
		maxRangeDays: n
	} : e.id === "week" && r !== void 0 ? {
		...e,
		maxRangeDays: r * 7
	} : { ...e });
}
function E(e, t) {
	let n = c(e.maxRangeDays);
	if (n === void 0 || t === void 0) return !1;
	let r = o(t);
	return r !== null && r > n;
}
function D(e, t, n) {
	if (t.length === 0) return;
	let r = e === void 0 ? -1 : t.findIndex((t) => t.id === e), i = r >= 0 ? t[r] : void 0;
	return i && !E(i, n) ? i.id : (r >= 0 ? t.slice(r + 1) : t).find((e) => !E(e, n))?.id ?? t.find((e) => !E(e, n))?.id;
}
function O(e) {
	if (!f(e)) return;
	let t = s(e.start), n = s(e.end);
	return t && n ? {
		start: t,
		end: n
	} : void 0;
}
//#endregion
//#region src/period-select/PeriodSelect.tsx
function k({ value: o, onChange: s, options: c, presetIds: l, dateRange: u, maxDayRangeDays: d, maxWeekRangeWeeks: f, ...p }) {
	let m = r(() => c ?? S(x(l), T({
		maxDayRangeDays: d,
		maxWeekRangeWeeks: f
	})), [
		d,
		f,
		c,
		l
	]), h = r(() => D(o, m, u), [
		u,
		m,
		o
	]), g = i(null), _ = n(() => {
		if (h === void 0 || h === o) {
			g.current = null;
			return;
		}
		let e = g.current;
		e && e.value === o && e.availableValue === h || (g.current = {
			value: o,
			availableValue: h
		}, s(h));
	});
	return t(() => {
		_();
	}, [h, o]), /* @__PURE__ */ a(e, {
		...p,
		options: m,
		value: h,
		onChange: s,
		getOptionDisabled: (e) => E(e, u)
	});
}
//#endregion
export { h as DEFAULT_PERIOD_SELECT_MAX_DAY_RANGE_DAYS, g as DEFAULT_PERIOD_SELECT_MAX_WEEK_RANGE_WEEKS, m as DEFAULT_PERIOD_SELECT_PRESET_ID, v as DEFAULT_PERIOD_SELECT_PRESET_IDS, p as PERIOD_SELECT_COMPONENT_ID, _ as PERIOD_SELECT_OPTIONS, k as PeriodSelect, T as createPeriodSelectOptions, C as getPeriodSelectOption, E as isPeriodSelectOptionDisabled, b as isPeriodSelectPresetId, O as normalizePeriodSelectDateRangeConnectors, x as normalizePeriodSelectPresetIds, D as resolvePeriodSelectAvailableValue, w as resolvePeriodSelectDefaultValue, S as resolvePeriodSelectOptionsByIds };

//# sourceMappingURL=index.js.map