{"version":3,"file":"toggleFont.cjs","names":[],"sources":["../../../src/font-library/utils/toggleFont.ts"],"sourcesContent":["/**\n * Toggles the activation of a given font or font variant within a list of custom fonts.\n *\n * - If only the font is provided (without face), the entire font family's activation is toggled.\n * - If both font and face are provided, the activation of the specific font variant is toggled.\n *\n * @param {Object} font            - The font to be toggled.\n * @param {string} font.slug       - The unique identifier for the font.\n * @param {Array}  [font.fontFace] - The list of font variants (faces) associated with the font.\n *\n * @param {Object} [face]          - The specific font variant to be toggled.\n * @param {string} face.fontWeight - The weight of the font variant.\n * @param {string} face.fontStyle  - The style of the font variant.\n *\n * @param {Array}  initialfonts    - The initial list of custom fonts.\n *\n * @return {Array} - The updated list of custom fonts with the font/font variant toggled.\n *\n * @example\n * const customFonts = [\n *     { slug: 'roboto', fontFace: [{ fontWeight: '400', fontStyle: 'normal' }] }\n * ];\n *\n * toggleFont({ slug: 'roboto' }, null, customFonts);\n * // This will remove 'roboto' from customFonts\n *\n * toggleFont({ slug: 'roboto' }, { fontWeight: '400', fontStyle: 'normal' }, customFonts);\n * // This will remove the specified face from 'roboto' in customFonts\n *\n * toggleFont({ slug: 'roboto' }, { fontWeight: '500', fontStyle: 'normal' }, customFonts);\n * // This will add the specified face to 'roboto' in customFonts\n */\nimport type { FontFamily, FontFace } from '@wordpress/core-data';\n\nexport function toggleFont(\n\tfont: FontFamily,\n\tface?: FontFace,\n\tinitialfonts: FontFamily[] = []\n): FontFamily[] {\n\t// Helper to check if a font is activated based on its slug\n\tconst isFontActivated = ( f: FontFamily ): boolean => f.slug === font.slug;\n\n\t// Helper to get the activated font from a list of fonts\n\tconst getActivatedFont = ( fonts: FontFamily[] ): FontFamily | undefined =>\n\t\tfonts.find( isFontActivated );\n\n\t// Toggle the activation status of an entire font family\n\tconst toggleEntireFontFamily = (\n\t\tactivatedFont: FontFamily | undefined\n\t): FontFamily[] => {\n\t\tif ( ! activatedFont ) {\n\t\t\t// If the font is not active, activate the entire font family\n\t\t\treturn [ ...initialfonts, font ];\n\t\t}\n\t\t// If the font is already active, deactivate the entire font family\n\t\treturn initialfonts.filter(\n\t\t\t( f: FontFamily ) => ! isFontActivated( f )\n\t\t);\n\t};\n\n\t// Toggle the activation status of a specific font variant\n\tconst toggleFontVariant = (\n\t\tactivatedFont: FontFamily | undefined\n\t): FontFamily[] => {\n\t\tconst isFaceActivated = ( f: FontFace ): boolean =>\n\t\t\tf.fontWeight === face!.fontWeight &&\n\t\t\tf.fontStyle === face!.fontStyle;\n\n\t\tif ( ! activatedFont ) {\n\t\t\t// If the font family is not active, activate the font family with the font variant\n\t\t\treturn [ ...initialfonts, { ...font, fontFace: [ face! ] } ];\n\t\t}\n\n\t\tlet newFontFaces = activatedFont.fontFace || [];\n\n\t\tif ( newFontFaces.find( isFaceActivated ) ) {\n\t\t\t// If the font variant is active, deactivate it\n\t\t\tnewFontFaces = newFontFaces.filter(\n\t\t\t\t( f: FontFace ) => ! isFaceActivated( f )\n\t\t\t);\n\t\t} else {\n\t\t\t// If the font variant is not active, activate it\n\t\t\tnewFontFaces = [ ...newFontFaces, face! ];\n\t\t}\n\n\t\t// If there are no more font faces, deactivate the font family\n\t\tif ( newFontFaces.length === 0 ) {\n\t\t\treturn initialfonts.filter(\n\t\t\t\t( f: FontFamily ) => ! isFontActivated( f )\n\t\t\t);\n\t\t}\n\n\t\t// Return updated fonts list with toggled font variant\n\t\treturn initialfonts.map( ( f: FontFamily ) =>\n\t\t\tisFontActivated( f ) ? { ...f, fontFace: newFontFaces } : f\n\t\t);\n\t};\n\n\tconst activatedFont = getActivatedFont( initialfonts );\n\n\tif ( ! face ) {\n\t\treturn toggleEntireFontFamily( activatedFont );\n\t}\n\n\treturn toggleFontVariant( activatedFont );\n}\n"],"mappings":";;;;;;AAkCA,SAAgB,WACf,MACA,MACA,eAA6B,CAAC,GACf;CAEf,MAAM,mBAAoB,MAA4B,EAAE,SAAS,KAAK;CAGtE,MAAM,oBAAqB,UAC1B,MAAM,KAAM,eAAgB;CAG7B,MAAM,0BACL,kBACkB;EAClB,IAAK,CAAE,eAEN,OAAO,CAAE,GAAG,cAAc,IAAK;EAGhC,OAAO,aAAa,QACjB,MAAmB,CAAE,gBAAiB,CAAE,CAC3C;CACD;CAGA,MAAM,qBACL,kBACkB;EAClB,MAAM,mBAAoB,MACzB,EAAE,eAAe,KAAM,cACvB,EAAE,cAAc,KAAM;EAEvB,IAAK,CAAE,eAEN,OAAO,CAAE,GAAG,cAAc;GAAE,GAAG;GAAM,UAAU,CAAE,IAAM;EAAE,CAAE;EAG5D,IAAI,eAAe,cAAc,YAAY,CAAC;EAE9C,IAAK,aAAa,KAAM,eAAgB,GAEvC,eAAe,aAAa,QACzB,MAAiB,CAAE,gBAAiB,CAAE,CACzC;OAGA,eAAe,CAAE,GAAG,cAAc,IAAM;EAIzC,IAAK,aAAa,WAAW,GAC5B,OAAO,aAAa,QACjB,MAAmB,CAAE,gBAAiB,CAAE,CAC3C;EAID,OAAO,aAAa,KAAO,MAC1B,gBAAiB,CAAE,IAAI;GAAE,GAAG;GAAG,UAAU;EAAa,IAAI,CAC3D;CACD;CAEA,MAAM,gBAAgB,iBAAkB,YAAa;CAErD,IAAK,CAAE,MACN,OAAO,uBAAwB,aAAc;CAG9C,OAAO,kBAAmB,aAAc;AACzC"}