import {createApplicationService} from "../Application/service/createApplicationService"; import {NoticeManager} from "./NoticeManager"; import {closeNoticeServiceHook, iNoticeServiceOption, nextNoticeId, NoticeDirection} from "./noitice.service.utils"; import {formatOptions} from "@peryl/utils/formatServiceOptions"; import {i18n} from "../../i18n"; import {ThemeStatus} from "../../uses/useStyle"; import {ThemeStatusIcons} from "../Application/theme/theme.utils"; export const useNotice = createApplicationService( 'notice', NoticeManager, (getManager) => { const service = (sourceOption: string | iNoticeServiceOption, externalOption?: iNoticeServiceOption): () => void => { const option = formatOptions(sourceOption, externalOption); option.id = nextNoticeId(); if (option.title === undefined) {option.title = () => i18n.$it('base.notice').d('提示');} if (option.time === undefined) {option.time = 3000;} if (option.status === undefined) {option.status = ThemeStatus.primary;} if (!!option.status && !option.icon) {option.icon = ThemeStatusIcons[option.status];} if (!option.horizontal) {option.horizontal = NoticeDirection.end;} if (!option.vertical) {option.vertical = NoticeDirection.start;} getManager().then(manager => manager.getContainer({ horizontal: option.horizontal!, vertical: option.vertical! }).then(container => container.getNotice(option))); return () => closeNoticeServiceHook.exec(option.id); }; function createStatusService(): Record { return Object.keys(ThemeStatus).reduce((prev, key) => { if (key !== 'TYPE') { prev[key] = (sourceOption: string | iNoticeServiceOption, externalOption?: iNoticeServiceOption) => { const option = formatOptions(sourceOption, externalOption); option.status = key as any; return service(option); }; } return prev; }, {} as any); } return { $notice: Object.assign(service, { ...createStatusService() }) }; }, );