{"version":3,"sources":["../src/constants.ts","../src/preview/decorators/withIntl.tsx","../src/config.ts","../src/state.ts","../src/utils/locale.ts","../src/preview/index.ts"],"names":["ADDON_ID","PANEL_ID","PARAMETER_KEY","GLOBALS_KEY","EVENTS","useChannel","useEffect","useGlobals","React","Fragment","IntlProvider","parseConfig","config","value","parseState","state","getActiveLocale","locales","activeLocale","defaultLocale","withIntl","StoryFn","context","parameters","rawIntlConfig","intlConfig","parseError","emit","eventData","globals","rawIntlState","intlState","getMessages","getFormats","defaultRichTextElements","messages","formats","preview","preview_default"],"mappings":"AAEO,IAAMA,EAAW,+BACXC,EAAW,GAAGD,CAAQ,SAEtBE,EAAgB,OAChBC,EAAc,OAEdC,EAAS,CAClB,cAAe,GAAGJ,CAAQ,gBAC9B,ECTA,OAAS,cAAAK,EAAY,aAAAC,EAAW,cAAAC,MAAkB,wBAClD,OAAOC,GAAS,YAAAC,MAAgB,QAChC,OAAS,gBAAAC,MAAoB,aCItB,SAASC,EAAYC,EAAoC,CAC5D,OAAI,OAAOA,GAAW,UAAYA,IAAW,KAClC,CAAE,OAAQ,KAAM,MAAO,SAAU,EAExC,EAAE,YAAaA,IACf,CAAC,MAAM,QAAQA,EAAO,OAAO,GAC7BA,EAAO,QAAQ,KAAMC,GAAU,OAAOA,GAAU,QAAQ,EAEjD,CAAE,OAAQ,KAAM,MAAO,iBAAkB,EAEhD,EAAE,kBAAmBD,IACrB,OAAOA,EAAO,eAAkB,SAEzB,CAAE,OAAQ,KAAM,MAAO,wBAAyB,EAEvD,EAAE,gBAAiBA,IACnB,OAAOA,EAAO,aAAgB,WAEvB,CAAE,OAAQ,KAAM,MAAO,sBAAuB,EAErD,eAAgBA,GAChB,OAAOA,EAAO,YAAe,WAEtB,CAAE,OAAQ,KAAM,MAAO,qBAAsB,EAEpD,4BAA6BA,IAC5B,OAAOA,EAAO,yBAA4B,UACvCA,EAAO,0BAA4B,MACnC,MAAM,QAAQA,EAAO,uBAAuB,GAEzC,CAAE,OAAQ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AAY,EACnCkB,EACF,OAAOH,GAAe,WAAaA,EAAWf,CAAY,EAAI,OAElE,OACIV,EAAA,cAACE,EAAA,CACG,OAAQQ,EACR,SAAUiB,EACV,QAASC,EACT,wBAAyBF,GAExBb,EAAQ,CACb,CAER,EIvEA,IAAMgB,EAAmB,CACrB,WAAY,CAACjB,CAAQ,EACrB,WAAY,CACR,CAAClB,CAAa,EAAG,IACrB,EACA,eAAgB,CACZ,CAACC,CAAW,EAAG,CACX,aAAc,IAClB,CACJ,CACJ,EAEOmC,EAAQD","sourcesContent":["import { StorybookAddonIntlConfigCommon } from './types';\n\nexport const ADDON_ID = 'truffls/storybook-addon-intl';\nexport const PANEL_ID = `${ADDON_ID}/panel`;\n\nexport const PARAMETER_KEY = 'intl';\nexport const GLOBALS_KEY = 'intl';\n\nexport const EVENTS = {\n    CONFIG_STATUS: `${ADDON_ID}/config_status`\n};\nexport type EventsData = {\n    CONFIG_STATUS: {\n        config: StorybookAddonIntlConfigCommon | null;\n        error: string | null;\n    };\n};\n","import type { Decorator } from '@storybook/react';\nimport { useChannel, useEffect, useGlobals } from 'storybook/preview-api';\nimport React, { Fragment } from 'react';\nimport { IntlProvider } from 'react-intl';\nimport { parseConfig } from '../../config';\nimport {\n    EVENTS,\n    EventsData,\n    GLOBALS_KEY,\n    PARAMETER_KEY\n} from '../../constants';\nimport { parseState } from '../../state';\nimport { getActiveLocale } from '../../utils/locale';\n\nexport const withIntl: Decorator = (StoryFn, context) => {\n    const { parameters } = context;\n    const rawIntlConfig = parameters[PARAMETER_KEY];\n    const { config: intlConfig, error: parseError } =\n        parseConfig(rawIntlConfig);\n\n    const emit = useChannel({});\n\n    useEffect(() => {\n        const eventData: EventsData['CONFIG_STATUS'] = {\n            config: intlConfig\n                ? {\n                      defaultLocale: intlConfig.defaultLocale,\n                      locales: intlConfig.locales\n                  }\n                : null,\n            error: null\n        };\n        emit(EVENTS.CONFIG_STATUS, eventData);\n    }, [intlConfig, parseError]);\n\n    const [globals] = useGlobals();\n    const rawIntlState = globals[GLOBALS_KEY];\n    const intlState = parseState(rawIntlState);\n\n    const locales = intlConfig?.locales ?? [];\n\n    const activeLocale = getActiveLocale(\n        locales,\n        intlState.activeLocale,\n        intlConfig?.defaultLocale\n    );\n\n    useEffect(() => {\n        if (!!parseError || !activeLocale) {\n            return;\n        }\n\n        document.documentElement.lang = activeLocale;\n    }, [activeLocale]);\n\n    if (!!parseError || !intlConfig || !activeLocale) {\n        return <Fragment />;\n    }\n\n    const { getMessages, getFormats, defaultRichTextElements } = intlConfig;\n\n    const messages = getMessages(activeLocale);\n    const formats =\n        typeof getFormats === 'function' ? getFormats(activeLocale) : undefined;\n\n    return (\n        <IntlProvider\n            locale={activeLocale}\n            messages={messages}\n            formats={formats}\n            defaultRichTextElements={defaultRichTextElements}\n        >\n            {StoryFn()}\n        </IntlProvider>\n    );\n};\n","import { StorybookAddonIntlConfig } from './types';\n\ntype ParseConfigResult = {\n    config: StorybookAddonIntlConfig | null;\n    error: string | null;\n};\n\nexport function parseConfig(config: unknown): ParseConfigResult {\n    if (typeof config !== 'object' || config === null) {\n        return { config: null, error: 'missing' };\n    } else if (\n        !('locales' in config) ||\n        !Array.isArray(config.locales) ||\n        config.locales.some((value) => typeof value !== 'string')\n    ) {\n        return { config: null, error: 'invalid-locales' };\n    } else if (\n        !('defaultLocale' in config) ||\n        typeof config.defaultLocale !== 'string'\n    ) {\n        return { config: null, error: 'invalid-default-locale' };\n    } else if (\n        !('getMessages' in config) ||\n        typeof config.getMessages !== 'function'\n    ) {\n        return { config: null, error: 'invalid-get-messages' };\n    } else if (\n        'getFormats' in config &&\n        typeof config.getFormats !== 'function'\n    ) {\n        return { config: null, error: 'invalid-get-formats' };\n    } else if (\n        'defaultRichTextElements' in config &&\n        (typeof config.defaultRichTextElements !== 'object' ||\n            config.defaultRichTextElements === null ||\n            Array.isArray(config.defaultRichTextElements))\n    ) {\n        return { config: null, error: 'invalid-rich-text-elements' };\n    }\n    return { config: config as StorybookAddonIntlConfig, error: null };\n}\n","import { StorybookAddonIntlState } from './types';\n\nexport function parseState(state: unknown): StorybookAddonIntlState {\n    if (\n        typeof state !== 'object' ||\n        state === null ||\n        !('activeLocale' in state) ||\n        typeof state.activeLocale !== 'string'\n    ) {\n        return { activeLocale: null };\n    }\n\n    return {\n        activeLocale: state.activeLocale\n    };\n}\n","export function getActiveLocale(\n    locales: string[],\n    activeLocale: string | null | undefined,\n    defaultLocale: string | null | undefined\n): string | null {\n    if (!!activeLocale && locales.includes(activeLocale)) {\n        return activeLocale;\n    }\n\n    if (!!defaultLocale && locales.includes(defaultLocale)) {\n        return defaultLocale;\n    }\n\n    return null;\n}\n","import type { Preview } from '@storybook/react';\nimport { GLOBALS_KEY, PARAMETER_KEY } from '../constants';\nimport { withIntl } from './decorators/withIntl';\n\nconst preview: Preview = {\n    decorators: [withIntl],\n    parameters: {\n        [PARAMETER_KEY]: null\n    },\n    initialGlobals: {\n        [GLOBALS_KEY]: {\n            activeLocale: null\n        }\n    }\n};\n\nexport default preview;\n"]}