{"version":3,"file":"useCalendar.cjs","sources":["../../../../../src/components/datepicker/internal/useCalendar.ts"],"sourcesContent":["import dayjs from \"dayjs\";\n/***\n * MIT License\n *\n * Copyright (c) 2017 Deseret Digital Media. 2022 Fremtind Forsikring AS.\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\nimport React, { useState } from \"react\";\nimport {\n    type CalendarMonth,\n    type DateInfo,\n    addMonth,\n    composeEventHandlers,\n    getCalendars,\n    isBackDisabled,\n    isForwardDisabled,\n    subtractMonth,\n} from \"./utils.js\";\n\nfunction isOffsetControlled(propOffset: number | undefined): boolean {\n    return propOffset !== undefined;\n}\n\nfunction getOffset(prop: number, state: number): number {\n    return isOffsetControlled(prop) ? prop : state;\n}\n\ntype BoundGetDateProps = (dateInfo: DateInfo, event: React.MouseEvent) => void;\n\ntype GetDateProps = {\n    onClick?: React.MouseEventHandler;\n    dateObj: DateInfo;\n};\n\ntype GetDatePropsResult = {\n    onClick: React.MouseEventHandler;\n    disabled: boolean;\n    \"aria-pressed\": boolean;\n    role: \"button\";\n};\n\nfunction getDateProps(\n    onDateSelected: BoundGetDateProps,\n    { onClick, dateObj }: GetDateProps,\n): GetDatePropsResult {\n    return {\n        onClick: composeEventHandlers(onClick, (event) => {\n            onDateSelected(dateObj, event);\n        }),\n        disabled: !dateObj.selectable,\n        \"aria-pressed\": dateObj.selected,\n        role: \"button\",\n    };\n}\n\ntype BoundGetBackProps = {\n    handleOffsetChanged: (newOffset: number) => void;\n    offsetMonth: number;\n    minDate?: Date;\n};\n\ntype GetBackProps = {\n    onClick?: React.MouseEventHandler;\n    offset?: number;\n    calendars: CalendarMonth[];\n};\n\ntype GetBackPropsResult = {\n    onClick: React.MouseEventHandler;\n    disabled: boolean;\n    \"aria-label\": string;\n    title: string;\n};\n\nfunction getBackProps(\n    { minDate, offsetMonth, handleOffsetChanged }: BoundGetBackProps,\n    { onClick, offset = 1, calendars }: GetBackProps,\n): GetBackPropsResult {\n    const label = `Gå tilbake ${offset} måned${offset === 1 ? \"\" : \"er\"}`;\n    return {\n        onClick: composeEventHandlers(onClick, () => {\n            handleOffsetChanged(\n                offsetMonth - subtractMonth({ calendars, offset, minDate }),\n            );\n        }),\n        disabled: isBackDisabled({ calendars, minDate }),\n        \"aria-label\": label,\n        title: label,\n    };\n}\n\ntype BoundGetForwardProps = {\n    handleOffsetChanged: (newOffset: number) => void;\n    offsetMonth: number;\n    maxDate?: Date;\n};\n\ntype GetForwardProps = {\n    onClick?: React.MouseEventHandler;\n    offset?: number;\n    calendars: CalendarMonth[];\n};\n\ntype GetForwardPropsResult = {\n    onClick: React.MouseEventHandler;\n    disabled: boolean;\n    \"aria-label\": string;\n    title: string;\n};\n\nfunction getForwardProps(\n    { maxDate, offsetMonth, handleOffsetChanged }: BoundGetForwardProps,\n    { onClick, offset = 1, calendars }: GetForwardProps,\n): GetForwardPropsResult {\n    const label = `Gå frem ${offset} måned${offset === 1 ? \"\" : \"er\"}`;\n    return {\n        onClick: composeEventHandlers(onClick, () => {\n            handleOffsetChanged(\n                offsetMonth + addMonth({ calendars, offset, maxDate }),\n            );\n        }),\n        disabled: isForwardDisabled({ calendars, maxDate }),\n        \"aria-label\": label,\n        title: label,\n    };\n}\n\nexport interface UseCalendarProps {\n    date?: Date;\n    maxDate?: Date;\n    minDate?: Date;\n    monthsToDisplay?: number;\n    firstDayOfWeek?: number;\n    showOutsideDays?: boolean;\n    offset: number;\n    onDateSelected: (dateObj: DateInfo, event: React.MouseEvent) => void;\n    onOffsetChanged: (newOffset: number) => void;\n    selected?: Date | Date[];\n}\n\nexport type GetDatePropsFunc = (props: GetDateProps) => GetDatePropsResult;\nexport type GetBackPropsFunc = (props: GetBackProps) => GetBackPropsResult;\nexport type GetForwardPropsFunc = (\n    props: GetForwardProps,\n) => GetForwardPropsResult;\nexport type HandleOffsetFunc = (newOffset: number) => void;\n\nexport type UseCalendarResult = {\n    calendars: CalendarMonth[];\n    getDateProps: GetDatePropsFunc;\n    getBackProps: GetBackPropsFunc;\n    getForwardProps: GetForwardPropsFunc;\n    handleOffsetChanged: HandleOffsetFunc;\n};\n\nexport function useCalendar({\n    date = dayjs().startOf(\"day\").toDate(),\n    maxDate,\n    minDate,\n    monthsToDisplay = 1,\n    firstDayOfWeek = 0,\n    showOutsideDays = true,\n    offset,\n    onDateSelected,\n    onOffsetChanged,\n    selected,\n}: UseCalendarProps): UseCalendarResult {\n    const [stateOffset, setStateOffset] = useState(0);\n    const offsetMonth = getOffset(offset, stateOffset);\n\n    function handleOffsetChanged(newOffset: number) {\n        if (!isOffsetControlled(offset)) {\n            setStateOffset(newOffset);\n        }\n        onOffsetChanged(newOffset);\n    }\n\n    const calendars = getCalendars({\n        date,\n        selected,\n        monthsToDisplay,\n        minDate,\n        maxDate,\n        offset: offsetMonth,\n        firstDayOfWeek,\n        showOutsideDays,\n    });\n    return {\n        calendars,\n        getDateProps: getDateProps.bind(null, onDateSelected),\n        getBackProps: getBackProps.bind(null, {\n            minDate,\n            offsetMonth,\n            handleOffsetChanged,\n        }),\n        getForwardProps: getForwardProps.bind(null, {\n            maxDate,\n            offsetMonth,\n            handleOffsetChanged,\n        }),\n        handleOffsetChanged,\n    };\n}\n"],"names":["isOffsetControlled","propOffset","getDateProps","onDateSelected","onClick","dateObj","composeEventHandlers","event","disabled","selectable","selected","role","getBackProps","minDate","offsetMonth","handleOffsetChanged","offset","calendars","label","subtractMonth","isBackDisabled","title","getForwardProps","maxDate","addMonth","isForwardDisabled","date","dayjs","startOf","toDate","monthsToDisplay","firstDayOfWeek","showOutsideDays","onOffsetChanged","stateOffset","setStateOffset","useState","prop","state","getOffset","newOffset","getCalendars","bind"],"mappings":"qJAoCA,SAASA,EAAmBC,GACxB,YAAsB,IAAfA,CACX,CAoBA,SAASC,EACLC,GACEC,QAAAA,EAASC,QAAAA,IAEX,MAAO,CACHD,QAASE,EAAAA,qBAAqBF,EAAUG,IACpCJ,EAAeE,EAASE,KAE5BC,UAAWH,EAAQI,WACnB,eAAgBJ,EAAQK,SACxBC,KAAM,SAEd,CAqBA,SAASC,GACHC,QAAAA,EAASC,YAAAA,EAAaC,oBAAAA,IACtBX,QAAAA,EAASY,OAAAA,EAAS,EAAGC,UAAAA,IAEvB,MAAMC,EAAQ,cAAcF,UAA0B,IAAXA,EAAe,GAAK,OAC/D,MAAO,CACHZ,QAASE,EAAAA,qBAAqBF,EAAS,KACnCW,EACID,EAAcK,EAAAA,cAAc,CAAEF,UAAAA,EAAWD,OAAAA,EAAQH,QAAAA,OAGzDL,SAAUY,EAAAA,eAAe,CAAEH,UAAAA,EAAWJ,QAAAA,IACtC,aAAcK,EACdG,MAAOH,EAEf,CAqBA,SAASI,GACHC,QAAAA,EAAST,YAAAA,EAAaC,oBAAAA,IACtBX,QAAAA,EAASY,OAAAA,EAAS,EAAGC,UAAAA,IAEvB,MAAMC,EAAQ,WAAWF,UAA0B,IAAXA,EAAe,GAAK,OAC5D,MAAO,CACHZ,QAASE,EAAAA,qBAAqBF,EAAS,KACnCW,EACID,EAAcU,EAAAA,SAAS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