{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/date-time/date-picker/use-lilius/index.ts"],"sourcesContent":["/**\n * This source is a local copy of the use-lilius library, since the original\n * library is not actively maintained.\n * @see https://github.com/WordPress/gutenberg/discussions/64968\n *\n * use-lilius@2.0.5\n * https://github.com/Avarios/use-lilius\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2021-Present Danny Tatom\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 {\n\taddMonths,\n\taddYears,\n\teachDayOfInterval,\n\teachMonthOfInterval,\n\teachWeekOfInterval,\n\tendOfMonth,\n\tendOfWeek,\n\tisAfter,\n\tisBefore,\n\tisEqual,\n\tset,\n\tsetMonth,\n\tsetYear,\n\tstartOfMonth,\n\tstartOfToday,\n\tstartOfWeek,\n\tsubMonths,\n\tsubYears,\n} from 'date-fns';\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n\nexport enum Month {\n\tJANUARY,\n\tFEBRUARY,\n\tMARCH,\n\tAPRIL,\n\tMAY,\n\tJUNE,\n\tJULY,\n\tAUGUST,\n\tSEPTEMBER,\n\tOCTOBER,\n\tNOVEMBER,\n\tDECEMBER,\n}\n\nexport enum Day {\n\tSUNDAY,\n\tMONDAY,\n\tTUESDAY,\n\tWEDNESDAY,\n\tTHURSDAY,\n\tFRIDAY,\n\tSATURDAY,\n}\n\nexport interface Options {\n\t/**\n\t * What day a week starts on within the calendar matrix.\n\t *\n\t * @default Day.SUNDAY\n\t */\n\tweekStartsOn?: Day;\n\n\t/**\n\t * The initial viewing date.\n\t *\n\t * @default new Date()\n\t */\n\tviewing?: Date;\n\n\t/**\n\t * The initial date(s) selection.\n\t *\n\t * @default []\n\t */\n\tselected?: Date[];\n\n\t/**\n\t * The number of months in the calendar.\n\t *\n\t * @default 1\n\t */\n\tnumberOfMonths?: number;\n}\n\nexport interface Returns {\n\t/**\n\t * Returns a copy of the given date with the time set to 00:00:00:00.\n\t */\n\tclearTime: ( date: Date ) => Date;\n\n\t/**\n\t * Returns whether or not a date is between 2 other dates (inclusive).\n\t */\n\tinRange: ( date: Date, min: Date, max: Date ) => boolean;\n\n\t/**\n\t * The date represented in the calendar matrix. Note that\n\t * the month and year are the only parts used.\n\t */\n\tviewing: Date;\n\n\t/**\n\t * Set the date represented in the calendar matrix. Note that\n\t * the month and year are the only parts used.\n\t */\n\tsetViewing: React.Dispatch< React.SetStateAction< Date > >;\n\n\t/**\n\t * Set the viewing date to today.\n\t */\n\tviewToday: () => void;\n\n\t/**\n\t * Set the viewing date to the given month.\n\t */\n\tviewMonth: ( month: Month ) => void;\n\n\t/**\n\t * Set the viewing date to the month before the current.\n\t */\n\tviewPreviousMonth: () => void;\n\n\t/**\n\t * Set the viewing date to the month after the current.\n\t */\n\tviewNextMonth: () => void;\n\n\t/**\n\t * Set the viewing date to the given year.\n\t */\n\tviewYear: ( year: number ) => void;\n\n\t/**\n\t * Set the viewing date to the year before the current.\n\t */\n\tviewPreviousYear: () => void;\n\n\t/**\n\t * Set the viewing date to the year after the current.\n\t */\n\tviewNextYear: () => void;\n\n\t/**\n\t * The dates currently selected.\n\t */\n\tselected: Date[];\n\n\t/**\n\t * Override the currently selected dates.\n\t */\n\tsetSelected: React.Dispatch< React.SetStateAction< Date[] > >;\n\n\t/**\n\t * Reset the selected dates to [].\n\t */\n\tclearSelected: () => void;\n\n\t/**\n\t * Determine whether or not a date has been selected.\n\t */\n\tisSelected: ( date: Date ) => boolean;\n\n\t/**\n\t * Select one or more dates.\n\t */\n\tselect: ( date: Date | Date[], replaceExisting?: boolean ) => void;\n\n\t/**\n\t * Deselect one or more dates.\n\t */\n\tdeselect: ( date: Date | Date[] ) => void;\n\n\t/**\n\t * Toggle the selection of a date.\n\t */\n\ttoggle: ( date: Date, replaceExisting?: boolean ) => void;\n\n\t/**\n\t * Select a range of dates (inclusive).\n\t */\n\tselectRange: ( start: Date, end: Date, replaceExisting?: boolean ) => void;\n\n\t/**\n\t * Deselect a range of dates (inclusive).\n\t */\n\tdeselectRange: ( start: Date, end: Date ) => void;\n\n\t/**\n\t * A matrix of days based on the current viewing date.\n\t */\n\tcalendar: Date[][][];\n}\n\nconst inRange = ( date: Date, min: Date, max: Date ) =>\n\t( isEqual( date, min ) || isAfter( date, min ) ) &&\n\t( isEqual( date, max ) || isBefore( date, max ) );\n\nconst clearTime = ( date: Date ) =>\n\tset( date, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 } );\n\nexport const useLilius = ( {\n\tweekStartsOn = Day.SUNDAY,\n\tviewing: initialViewing = new Date(),\n\tselected: initialSelected = [],\n\tnumberOfMonths = 1,\n}: Options = {} ): Returns => {\n\tconst [ viewing, setViewing ] = useState< Date >( initialViewing );\n\n\tconst viewToday = useCallback(\n\t\t() => setViewing( startOfToday() ),\n\t\t[ setViewing ]\n\t);\n\n\tconst viewMonth = useCallback(\n\t\t( month: Month ) => setViewing( ( v ) => setMonth( v, month ) ),\n\t\t[]\n\t);\n\n\tconst viewPreviousMonth = useCallback(\n\t\t() => setViewing( ( v ) => subMonths( v, 1 ) ),\n\t\t[]\n\t);\n\n\tconst viewNextMonth = useCallback(\n\t\t() => setViewing( ( v ) => addMonths( v, 1 ) ),\n\t\t[]\n\t);\n\n\tconst viewYear = useCallback(\n\t\t( year: number ) => setViewing( ( v ) => setYear( v, year ) ),\n\t\t[]\n\t);\n\n\tconst viewPreviousYear = useCallback(\n\t\t() => setViewing( ( v ) => subYears( v, 1 ) ),\n\t\t[]\n\t);\n\n\tconst viewNextYear = useCallback(\n\t\t() => setViewing( ( v ) => addYears( v, 1 ) ),\n\t\t[]\n\t);\n\n\tconst [ selected, setSelected ] = useState< Date[] >(\n\t\tinitialSelected.map( clearTime )\n\t);\n\n\tconst clearSelected = () => setSelected( [] );\n\n\tconst isSelected = useCallback(\n\t\t( date: Date ) =>\n\t\t\tselected.findIndex( ( s ) => isEqual( s, date ) ) > -1,\n\t\t[ selected ]\n\t);\n\n\tconst select = useCallback(\n\t\t( date: Date | Date[], replaceExisting?: boolean ) => {\n\t\t\tif ( replaceExisting ) {\n\t\t\t\tsetSelected( Array.isArray( date ) ? date : [ date ] );\n\t\t\t} else {\n\t\t\t\tsetSelected( ( selectedItems ) =>\n\t\t\t\t\tselectedItems.concat(\n\t\t\t\t\t\tArray.isArray( date ) ? date : [ date ]\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[]\n\t);\n\n\tconst deselect = useCallback(\n\t\t( date: Date | Date[] ) =>\n\t\t\tsetSelected( ( selectedItems ) =>\n\t\t\t\tArray.isArray( date )\n\t\t\t\t\t? selectedItems.filter(\n\t\t\t\t\t\t\t( s ) =>\n\t\t\t\t\t\t\t\t! date\n\t\t\t\t\t\t\t\t\t.map( ( d ) => d.getTime() )\n\t\t\t\t\t\t\t\t\t.includes( s.getTime() )\n\t\t\t\t\t\t)\n\t\t\t\t\t: selectedItems.filter( ( s ) => ! isEqual( s, date ) )\n\t\t\t),\n\t\t[]\n\t);\n\n\tconst toggle = useCallback(\n\t\t( date: Date, replaceExisting?: boolean ) =>\n\t\t\tisSelected( date )\n\t\t\t\t? deselect( date )\n\t\t\t\t: select( date, replaceExisting ),\n\t\t[ deselect, isSelected, select ]\n\t);\n\n\tconst selectRange = useCallback(\n\t\t( start: Date, end: Date, replaceExisting?: boolean ) => {\n\t\t\tif ( replaceExisting ) {\n\t\t\t\tsetSelected( eachDayOfInterval( { start, end } ) );\n\t\t\t} else {\n\t\t\t\tsetSelected( ( selectedItems ) =>\n\t\t\t\t\tselectedItems.concat( eachDayOfInterval( { start, end } ) )\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[]\n\t);\n\n\tconst deselectRange = useCallback( ( start: Date, end: Date ) => {\n\t\tsetSelected( ( selectedItems ) =>\n\t\t\tselectedItems.filter(\n\t\t\t\t( s ) =>\n\t\t\t\t\t! eachDayOfInterval( { start, end } )\n\t\t\t\t\t\t.map( ( d ) => d.getTime() )\n\t\t\t\t\t\t.includes( s.getTime() )\n\t\t\t)\n\t\t);\n\t}, [] );\n\n\tconst calendar = useMemo< Date[][][] >(\n\t\t() =>\n\t\t\teachMonthOfInterval( {\n\t\t\t\tstart: startOfMonth( viewing ),\n\t\t\t\tend: endOfMonth( addMonths( viewing, numberOfMonths - 1 ) ),\n\t\t\t} ).map( ( month ) =>\n\t\t\t\teachWeekOfInterval(\n\t\t\t\t\t{\n\t\t\t\t\t\tstart: startOfMonth( month ),\n\t\t\t\t\t\tend: endOfMonth( month ),\n\t\t\t\t\t},\n\t\t\t\t\t{ weekStartsOn }\n\t\t\t\t).map( ( week ) =>\n\t\t\t\t\teachDayOfInterval( {\n\t\t\t\t\t\tstart: startOfWeek( week, { weekStartsOn } ),\n\t\t\t\t\t\tend: endOfWeek( week, { weekStartsOn } ),\n\t\t\t\t\t} )\n\t\t\t\t)\n\t\t\t),\n\t\t[ viewing, weekStartsOn, numberOfMonths ]\n\t);\n\n\treturn {\n\t\tclearTime,\n\t\tinRange,\n\t\tviewing,\n\t\tsetViewing,\n\t\tviewToday,\n\t\tviewMonth,\n\t\tviewPreviousMonth,\n\t\tviewNextMonth,\n\t\tviewYear,\n\t\tviewPreviousYear,\n\t\tviewNextYear,\n\t\tselected,\n\t\tsetSelected,\n\t\tclearSelected,\n\t\tisSelected,\n\t\tselect,\n\t\tdeselect,\n\t\ttoggle,\n\t\tselectRange,\n\t\tdeselectRange,\n\t\tcalendar,\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,IAAY,QAAL,yBAAA,OAAA;CACN,MAAA,MAAA,aAAA,KAAA;CACA,MAAA,MAAA,cAAA,KAAA;CACA,MAAA,MAAA,WAAA,KAAA;CACA,MAAA,MAAA,WAAA,KAAA;CACA,MAAA,MAAA,SAAA,KAAA;CACA,MAAA,MAAA,UAAA,KAAA;CACA,MAAA,MAAA,UAAA,KAAA;CACA,MAAA,MAAA,YAAA,KAAA;CACA,MAAA,MAAA,eAAA,KAAA;CACA,MAAA,MAAA,aAAA,KAAA;CACA,MAAA,MAAA,cAAA,MAAA;CACA,MAAA,MAAA,cAAA,MAAA;;AACD,EAAA,CAAA,CAAA;AAEA,IAAY,MAAL,yBAAA,KAAA;CACN,IAAA,IAAA,YAAA,KAAA;CACA,IAAA,IAAA,YAAA,KAAA;CACA,IAAA,IAAA,aAAA,KAAA;CACA,IAAA,IAAA,eAAA,KAAA;CACA,IAAA,IAAA,cAAA,KAAA;CACA,IAAA,IAAA,YAAA,KAAA;CACA,IAAA,IAAA,cAAA,KAAA;;AACD,EAAA,CAAA,CAAA;AA6IA,MAAM,WAAY,MAAY,KAAW,SACtC,QAAS,MAAM,GAAI,KAAK,QAAS,MAAM,GAAI,OAC3C,QAAS,MAAM,GAAI,KAAK,SAAU,MAAM,GAAI;AAE/C,MAAM,aAAc,SACnB,IAAK,MAAM;CAAE,OAAO;CAAG,SAAS;CAAG,SAAS;CAAG,cAAc;AAAE,CAAE;AAElE,MAAa,aAAc,EAC1B,eAAA,GACA,SAAS,iCAAiB,IAAI,KAAK,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