{"version":3,"file":"NotificationReducer.cjs","sources":["../../../../src/lib/Notification/NotificationReducer.ts"],"sourcesContent":["import {without} from '@/internal/utils/without.ts';\n\n/**\n * Action names collection\n * @example\n * import { Actions } from './NotificationReducer';\n * dispatch({ type: Notification.NOTIFICATION_CLOSE})\n * dispatch({ type: Notification.NOTIFICATION_OPEN})\n * */\nexport enum Actions {\n    NOTIFICATION_CLOSE = 'NOTIFICATION_CLOSE',\n    NOTIFICATION_OPEN = 'NOTIFICATION_OPEN',\n    NOTIFICATION_REGISTER = 'NOTIFICATION_REGISTER',\n    NOTIFICATION_RE_OPEN = 'NOTIFICATION_RE_OPEN',\n}\n\n/** Dialog ID type */\nexport type Id = string;\n\nexport type NotificationProps = {\n    /**\n     * Defines a title for the notification, which is shown at the top of the notification window\n     * @see https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#title\n     */\n    title: string;\n    /**\n     * URL of an image to be displayed in the notification\n     * @see https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#icon\n     */\n    icon?: string;\n    /**\n     * Body text of the notification, which is displayed below the title\n     * @see https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#body\n     */\n    body?: string;\n    /**\n     * Indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.\n     * @see https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#requireinteraction\n     */\n    requireInteraction?: boolean;\n};\n\n/** Dialog state type */\nexport type NotificationState = {\n    /** List of open modals' IDs */\n    open: Id[];\n    props: Record<Id, NotificationProps>;\n    instances: Record<Id, Notification>;\n};\n\nexport const initialState = {\n    open: [],\n    props: {},\n    instances: {},\n} as NotificationState;\n\ntype CloseAction = {\n    /** ID of target Dialog */\n    id: Id;\n    /** Action name */\n    type: Actions.NOTIFICATION_CLOSE;\n};\n\ntype OpenAction = {\n    /** ID of target Dialog */\n    id: Id;\n    /** Action name */\n    type: Actions.NOTIFICATION_OPEN;\n    instance: Notification;\n};\n\ntype RegisterAction = {\n    /** ID of target Dialog */\n    id: Id;\n    /** Action name */\n    type: Actions.NOTIFICATION_REGISTER;\n    props: NotificationProps;\n};\n\nexport type NotificationAction = CloseAction | OpenAction | RegisterAction;\n\nconst omit = <TObject extends Record<string, unknown>>(obj: TObject, key: keyof TObject) => {\n    const nextObject = {...obj};\n    delete nextObject[key];\n    return nextObject;\n};\n\nexport const NotificationReducer = (\n    state = initialState,\n    action: NotificationAction\n): NotificationState => {\n    switch (action.type) {\n        case Actions.NOTIFICATION_CLOSE: {\n            const {id} = action;\n            const nextOpen = without(id, state.open);\n            const nextInstances = omit(state.instances, id);\n            return {\n                ...state,\n                open: nextOpen,\n                instances: nextInstances,\n            };\n        }\n        case Actions.NOTIFICATION_REGISTER: {\n            const {id, props} = action;\n            return {\n                ...state,\n                props: {\n                    ...state.props,\n                    [id]: props,\n                },\n            };\n        }\n        case Actions.NOTIFICATION_OPEN: {\n            const {id, instance} = action;\n            return {\n                ...state,\n                open: [...without(id, state.open), id],\n                instances: {\n                    ...state.instances,\n                    [id]: instance,\n                },\n            };\n        }\n        default:\n            return state;\n    }\n};\n"],"names":["Actions","initialState","omit","obj","key","nextObject","NotificationReducer","state","action","id","nextOpen","without","nextInstances","props","instance"],"mappings":"iJASY,IAAAA,GAAAA,IACRA,EAAA,mBAAqB,qBACrBA,EAAA,kBAAoB,oBACpBA,EAAA,sBAAwB,wBACxBA,EAAA,qBAAuB,uBAJfA,IAAAA,GAAA,CAAA,CAAA,EAyCL,MAAMC,EAAe,CACxB,KAAM,CAAC,EACP,MAAO,CAAC,EACR,UAAW,CAAA,CACf,EA2BMC,EAAO,CAA0CC,EAAcC,IAAuB,CAClF,MAAAC,EAAa,CAAC,GAAGF,CAAG,EAC1B,cAAOE,EAAWD,CAAG,EACdC,CACX,EAEaC,EAAsB,CAC/BC,EAAQN,EACRO,IACoB,CACpB,OAAQA,EAAO,KAAM,CACjB,IAAK,qBAA4B,CACvB,KAAA,CAAC,GAAAC,GAAMD,EACPE,EAAWC,EAAA,QAAQF,EAAIF,EAAM,IAAI,EACjCK,EAAgBV,EAAKK,EAAM,UAAWE,CAAE,EACvC,MAAA,CACH,GAAGF,EACH,KAAMG,EACN,UAAWE,CACf,CAAA,CAEJ,IAAK,wBAA+B,CAC1B,KAAA,CAAC,GAAAH,EAAI,MAAAI,CAAA,EAASL,EACb,MAAA,CACH,GAAGD,EACH,MAAO,CACH,GAAGA,EAAM,MACT,CAACE,CAAE,EAAGI,CAAA,CAEd,CAAA,CAEJ,IAAK,oBAA2B,CACtB,KAAA,CAAC,GAAAJ,EAAI,SAAAK,CAAA,EAAYN,EAChB,MAAA,CACH,GAAGD,EACH,KAAM,CAAC,GAAGI,UAAQF,EAAIF,EAAM,IAAI,EAAGE,CAAE,EACrC,UAAW,CACP,GAAGF,EAAM,UACT,CAACE,CAAE,EAAGK,CAAA,CAEd,CAAA,CAEJ,QACW,OAAAP,CAAA,CAEnB"}