{"version":3,"file":"utils.js","sources":["../../../../../../packages/components/date-picker/src/utils.ts"],"sourcesContent":["import dayjs from 'dayjs'\nimport { isArray } from '@element-plus/utils'\nimport { rangeArr } from '@element-plus/components/time-picker'\n\nimport type { Dayjs } from 'dayjs'\nimport type { DateCell } from './date-picker.type'\nimport type { DisabledDateType } from './props/shared'\n\ntype DayRange = [Dayjs | undefined, Dayjs | undefined]\n\nexport const isValidRange = (range: DayRange): boolean => {\n if (!isArray(range)) return false\n\n const [left, right] = range\n\n return (\n dayjs.isDayjs(left) && dayjs.isDayjs(right) && left.isSameOrBefore(right)\n )\n}\n\ntype GetDefaultValueParams = {\n lang: string\n unit: 'month' | 'year'\n unlinkPanels: boolean\n}\n\nexport type DefaultValue = [Date, Date] | Date | undefined\n\nexport const getDefaultValue = (\n defaultValue: DefaultValue,\n { lang, unit, unlinkPanels }: GetDefaultValueParams\n) => {\n let start: Dayjs\n\n if (isArray(defaultValue)) {\n let [left, right] = defaultValue.map((d) => dayjs(d).locale(lang))\n if (!unlinkPanels) {\n right = left.add(1, unit)\n }\n return [left, right]\n } else if (defaultValue) {\n start = dayjs(defaultValue)\n } else {\n start = dayjs()\n }\n start = start.locale(lang)\n return [start, start.add(1, unit)]\n}\n\ntype Dimension = {\n row: number\n column: number\n}\n\ntype BuildPickerTableMetadata = {\n startDate?: Dayjs | null\n unit: 'month' | 'day'\n columnIndexOffset: number\n now: Dayjs\n nextEndDate: Dayjs | null\n relativeDateGetter: (index: number) => Dayjs\n setCellMetadata?: (\n cell: DateCell,\n dimension: { rowIndex: number; columnIndex: number }\n ) => void\n setRowMetadata?: (row: DateCell[]) => void\n}\n\nexport const buildPickerTable = (\n dimension: Dimension,\n rows: DateCell[][],\n {\n columnIndexOffset,\n startDate,\n nextEndDate,\n now,\n unit,\n relativeDateGetter,\n setCellMetadata,\n setRowMetadata,\n }: BuildPickerTableMetadata\n) => {\n for (let rowIndex = 0; rowIndex < dimension.row; rowIndex++) {\n const row = rows[rowIndex]\n for (let columnIndex = 0; columnIndex < dimension.column; columnIndex++) {\n let cell = row[columnIndex + columnIndexOffset]\n if (!cell) {\n cell = {\n row: rowIndex,\n column: columnIndex,\n type: 'normal',\n inRange: false,\n start: false,\n end: false,\n }\n }\n const index = rowIndex * dimension.column + columnIndex\n const nextStartDate = relativeDateGetter(index)\n cell.dayjs = nextStartDate\n cell.date = nextStartDate.toDate()\n cell.timestamp = nextStartDate.valueOf()\n cell.type = 'normal'\n\n cell.inRange =\n !!(\n startDate &&\n nextStartDate.isSameOrAfter(startDate, unit) &&\n nextEndDate &&\n nextStartDate.isSameOrBefore(nextEndDate, unit)\n ) ||\n !!(\n startDate &&\n nextStartDate.isSameOrBefore(startDate, unit) &&\n nextEndDate &&\n nextStartDate.isSameOrAfter(nextEndDate, unit)\n )\n\n if (startDate?.isSameOrAfter(nextEndDate)) {\n cell.start = !!nextEndDate && nextStartDate.isSame(nextEndDate, unit)\n cell.end = startDate && nextStartDate.isSame(startDate, unit)\n } else {\n cell.start = !!startDate && nextStartDate.isSame(startDate, unit)\n cell.end = !!nextEndDate && nextStartDate.isSame(nextEndDate, unit)\n }\n\n const isToday = nextStartDate.isSame(now, unit)\n\n if (isToday) {\n cell.type = 'today'\n }\n setCellMetadata?.(cell, { rowIndex, columnIndex })\n row[columnIndex + columnIndexOffset] = cell\n }\n setRowMetadata?.(row)\n }\n}\n\nexport const datesInMonth = (year: number, month: number, lang: string) => {\n const firstDay = dayjs().locale(lang).startOf('month').month(month).year(year)\n const numOfDays = firstDay.daysInMonth()\n return rangeArr(numOfDays).map((n) => firstDay.add(n, 'day').toDate())\n}\n\nexport const getValidDateOfMonth = (\n year: number,\n month: number,\n lang: string,\n disabledDate?: DisabledDateType\n) => {\n const _value = dayjs().year(year).month(month).startOf('month')\n const _date = datesInMonth(year, month, lang).find((date) => {\n return !disabledDate?.(date)\n })\n if (_date) {\n return dayjs(_date).locale(lang)\n }\n return _value.locale(lang)\n}\n\nexport const getValidDateOfYear = (\n value: Dayjs,\n lang: string,\n disabledDate?: DisabledDateType\n) => {\n const year = value.year()\n if (!disabledDate?.(value.toDate())) {\n return value.locale(lang)\n }\n const month = value.month()\n if (!datesInMonth(year, month, lang).every(disabledDate)) {\n return getValidDateOfMonth(year, month, lang, disabledDate)\n }\n for (let i = 0; i < 12; i++) {\n if (!datesInMonth(year, i, lang).every(disabledDate)) {\n return getValidDateOfMonth(year, i, lang, disabledDate)\n }\n }\n return value\n}\n"],"names":["isArray","dayjs","rangeArr"],"mappings":";;;;;;;;;;;;;;AAGY,MAAC,YAAY,GAAG,CAAC,KAAK,KAAK;AACvC,EAAE,IAAI,CAACA,cAAO,CAAC,KAAK,CAAC;AACrB,IAAI,OAAO,KAAK,CAAC;AACjB,EAAE,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC;AAC9B,EAAE,OAAOC,yBAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAIA,yBAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;AACnF,EAAE;AACU,MAAC,eAAe,GAAG,CAAC,YAAY,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK;AAC/E,EAAE,IAAI,KAAK,CAAC;AACZ,EAAE,IAAID,cAAO,CAAC,YAAY,CAAC,EAAE;AAC7B,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,KAAKC,yBAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACvE,IAAI,IAAI,CAAC,YAAY,EAAE;AACvB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AAChC,KAAK;AACL,IAAI,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACzB,GAAG,MAAM,IAAI,YAAY,EAAE;AAC3B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