import { parseBooleanToken } from "@open-mercato/shared/lib/boolean";
const ID_COLUMN = "id";
const STAFF_MEMBER_COLUMN = "staff_member_id";
const DATE_COLUMN = "date";
const STARTED_AT_COLUMN = "started_at";
const ENDED_AT_COLUMN = "ended_at";
const PROJECT_COLUMN = "time_project_id";
function isParseableDateFilter(value) {
  const trimmed = value.trim();
  if (trimmed.length === 0) return true;
  if (!/^\d{4}-\d{2}-\d{2}([T ].*)?$/.test(trimmed)) return false;
  return !Number.isNaN(new Date(trimmed).getTime());
}
function buildTimeEntryListFilters(query) {
  const filters = {};
  if (typeof query.ids === "string" && query.ids.trim().length > 0) {
    const ids = query.ids.split(",").map((value) => value.trim()).filter((value) => value.length > 0);
    if (ids.length > 0) {
      filters[ID_COLUMN] = { $in: ids };
    }
  }
  if (typeof query.staffMemberId === "string" && query.staffMemberId.length > 0) {
    filters[STAFF_MEMBER_COLUMN] = query.staffMemberId;
  }
  if (typeof query.from === "string" && query.from.length > 0 && isParseableDateFilter(query.from)) {
    filters[DATE_COLUMN] = { ...filters[DATE_COLUMN] ?? {}, $gte: query.from };
  }
  if (typeof query.to === "string" && query.to.length > 0 && isParseableDateFilter(query.to)) {
    filters[DATE_COLUMN] = { ...filters[DATE_COLUMN] ?? {}, $lte: query.to };
  }
  if (typeof query.projectId === "string" && query.projectId.length > 0) {
    filters[PROJECT_COLUMN] = query.projectId;
  }
  if (parseBooleanToken(query.running ?? null) === true) {
    filters[STARTED_AT_COLUMN] = { $ne: null };
    filters[ENDED_AT_COLUMN] = null;
  }
  return filters;
}
export {
  buildTimeEntryListFilters,
  isParseableDateFilter
};
//# sourceMappingURL=timeEntryListFilters.js.map
