{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["import { _getDateValue } from \"../_dateValue/index.ts\";\nimport type { TDateInput } from \"../_dateValue/index.ts\";\n\ntype TDateUnit = \"millisecond\" | \"second\" | \"minute\" | \"hour\" | \"day\" | \"week\" | \"month\" | \"year\";\n\nexport type TGetDateAddedArgs = Parameters<typeof getDateAdded>;\n\nexport type TGetDateAddedReturn = ReturnType<typeof getDateAdded>;\n\nconst getDaysInMonth = (year: number, month: number): number => {\n  return new Date(year, month + 1, 0).getDate();\n};\n\n/**\n * Adds a calendar or elapsed-time unit to a date without mutating it.\n * Month and year additions clamp the day to the target month.\n * @param {TDateInput} date Source date\n * @param {number} amount Integer amount to add; may be negative\n * @param {TDateUnit} [unit=\"day\"] Unit\n * @returns {Date} New Date instance\n * @throws {TypeError} getDateAdded: date must be a valid date\n * @throws {TypeError} getDateAdded: amount must be a finite integer\n * @throws {TypeError} getDateAdded: unit is invalid\n * @example\n * getDateAdded(new Date(\"2024-01-31\"), 1, \"month\"); // 2024-02-29\n * @example\n * // Calculate a session expiration date seven days from its creation\n * const expiresAt = getDateAdded(session.createdAt, 7, \"day\");\n */\nexport const getDateAdded = (\n  date: TDateInput,\n  amount: number,\n  unit: TDateUnit = \"day\"\n): Date => {\n  if (!Number.isInteger(amount) || !Number.isFinite(amount)) {\n    throw new TypeError(\"getDateAdded: amount must be a finite integer\");\n  }\n  const units: TDateUnit[] = [\n    \"millisecond\", \"second\", \"minute\", \"hour\", \"day\", \"week\", \"month\", \"year\",\n  ];\n  if (!units.includes(unit)) {\n    throw new TypeError(`getDateAdded: unit must be one of: ${units.join(\", \")}`);\n  }\n\n  const result = _getDateValue(date, getDateAdded.name);\n  const multipliers: Partial<Record<TDateUnit, number>> = {\n    millisecond: 1,\n    second: 1_000,\n    minute: 60_000,\n    hour: 3_600_000,\n    day: 86_400_000,\n    week: 604_800_000,\n  };\n  const multiplier = multipliers[unit];\n  if (multiplier !== undefined) {\n    result.setTime(result.getTime() + amount * multiplier);\n    return result;\n  }\n\n  const originalDay = result.getDate();\n  result.setDate(1);\n  if (unit === \"month\") {\n    result.setMonth(result.getMonth() + amount);\n  } else {\n    result.setFullYear(result.getFullYear() + amount);\n  }\n  result.setDate(Math.min(originalDay, getDaysInMonth(result.getFullYear(), result.getMonth())));\n  return result;\n};\n"],"names":["getDaysInMonth","year","month","Date","getDate","getDateAdded","date","amount","unit","Number","isInteger","isFinite","TypeError","units","includes","join","result","_getDateValue","name","multipliers","millisecond","second","minute","hour","day","week","multiplier","undefined","setTime","getTime","originalDay","setDate","setMonth","getMonth","setFullYear","getFullYear","Math","min"],"mappings":"sGASA,MAAMA,eAAiB,CAACC,KAAcC,QAC7B,IAAIC,KAAKF,KAAMC,MAAQ,EAAG,GAAGE,UAmB/B,MAAMC,aAAe,CAC1BC,KACAC,OACAC,KAAkB,SAElB,IAAKC,OAAOC,UAAUH,UAAYE,OAAOE,SAASJ,QAChD,MAAM,IAAIK,UAAU,iDAEtB,MAAMC,MAAqB,CACzB,cAAe,SAAU,SAAU,OAAQ,MAAO,OAAQ,QAAS,QAErE,IAAKA,MAAMC,SAASN,MAClB,MAAM,IAAII,UAAU,sCAAsCC,MAAME,KAAK,SAGvE,MAAMC,OAASC,MAAAA,cAAcX,KAAMD,aAAaa,MAChD,MAAMC,YAAkD,CACtDC,YAAa,EACbC,OAAQ,MACRC,OAAQ,OACRC,KAAM,UACNC,IAAK,WACLC,KAAM,aAER,MAAMC,WAAaP,YAAYX,MAC/B,GAAIkB,kBAAeC,EAAW,CAC5BX,OAAOY,QAAQZ,OAAOa,UAAYtB,OAASmB,YAC3C,OAAOV,MACT,CAEA,MAAMc,YAAcd,OAAOZ,UAC3BY,OAAOe,QAAQ,GACf,GAAIvB,OAAS,QACXQ,OAAOgB,SAAShB,OAAOiB,WAAa1B,aAEpCS,OAAOkB,YAAYlB,OAAOmB,cAAgB5B,QAE5CS,OAAOe,QAAQK,KAAKC,IAAIP,YAAa9B,eAAegB,OAAOmB,cAAenB,OAAOiB,cACjF,OAAOjB"}