{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/box-control/utils.ts"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport { parseQuantityAndUnitFromRawValue } from '../unit-control/utils';\nimport type {\n\tBoxControlInputControlProps,\n\tBoxControlProps,\n\tBoxControlValue,\n\tCustomValueUnits,\n\tPreset,\n} from './types';\n\nexport const CUSTOM_VALUE_SETTINGS: CustomValueUnits = {\n\tpx: { max: 300, step: 1 },\n\t'%': { max: 100, step: 1 },\n\tvw: { max: 100, step: 1 },\n\tvh: { max: 100, step: 1 },\n\tem: { max: 10, step: 0.1 },\n\trm: { max: 10, step: 0.1 },\n\tsvw: { max: 100, step: 1 },\n\tlvw: { max: 100, step: 1 },\n\tdvw: { max: 100, step: 1 },\n\tsvh: { max: 100, step: 1 },\n\tlvh: { max: 100, step: 1 },\n\tdvh: { max: 100, step: 1 },\n\tvi: { max: 100, step: 1 },\n\tsvi: { max: 100, step: 1 },\n\tlvi: { max: 100, step: 1 },\n\tdvi: { max: 100, step: 1 },\n\tvb: { max: 100, step: 1 },\n\tsvb: { max: 100, step: 1 },\n\tlvb: { max: 100, step: 1 },\n\tdvb: { max: 100, step: 1 },\n\tvmin: { max: 100, step: 1 },\n\tsvmin: { max: 100, step: 1 },\n\tlvmin: { max: 100, step: 1 },\n\tdvmin: { max: 100, step: 1 },\n\tvmax: { max: 100, step: 1 },\n\tsvmax: { max: 100, step: 1 },\n\tlvmax: { max: 100, step: 1 },\n\tdvmax: { max: 100, step: 1 },\n\tcqw: { max: 100, step: 1 },\n\tcqh: { max: 100, step: 1 },\n\tcqi: { max: 100, step: 1 },\n\tcqb: { max: 100, step: 1 },\n\tcqmin: { max: 100, step: 1 },\n\tcqmax: { max: 100, step: 1 },\n};\n\nexport const LABELS = {\n\tall: __( 'All sides' ),\n\ttop: __( 'Top side' ),\n\tbottom: __( 'Bottom side' ),\n\tleft: __( 'Left side' ),\n\tright: __( 'Right side' ),\n\tvertical: __( 'Top and bottom sides' ),\n\thorizontal: __( 'Left and right sides' ),\n};\n\nexport const DEFAULT_VALUES = {\n\ttop: undefined,\n\tright: undefined,\n\tbottom: undefined,\n\tleft: undefined,\n};\n\nexport const ALL_SIDES = [ 'top', 'right', 'bottom', 'left' ] as const;\n\n/**\n * Gets the merged input value and unit from values data.\n *\n * @param values         Box values.\n * @param availableSides Available box sides to evaluate.\n *\n * @return A value + unit for the 'all' input.\n */\nexport function getMergedValue(\n\tvalues: BoxControlValue = {},\n\tavailableSides: BoxControlProps[ 'sides' ] = ALL_SIDES\n) {\n\tconst sides = normalizeSides( availableSides );\n\tif (\n\t\tsides.every(\n\t\t\t( side: keyof BoxControlValue ) =>\n\t\t\t\tvalues[ side ] === values[ sides[ 0 ] ]\n\t\t)\n\t) {\n\t\treturn values[ sides[ 0 ] ];\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Checks if the values are mixed.\n *\n * @param values         Box values.\n * @param availableSides Available box sides to evaluate.\n * @return Whether the values are mixed.\n */\nexport function isValueMixed(\n\tvalues: BoxControlValue = {},\n\tavailableSides: BoxControlProps[ 'sides' ] = ALL_SIDES\n) {\n\tconst sides = normalizeSides( availableSides );\n\treturn sides.some(\n\t\t( side: keyof BoxControlValue ) =>\n\t\t\tvalues[ side ] !== values[ sides[ 0 ] ]\n\t);\n}\n\n/**\n * Checks to determine if values are defined.\n *\n * @param values Box values.\n *\n * @return  Whether values are mixed.\n */\nexport function isValuesDefined( values?: BoxControlValue ) {\n\treturn (\n\t\tvalues &&\n\t\tObject.values( values ).filter(\n\t\t\t// Switching units when input is empty causes values only\n\t\t\t// containing units. This gives false positive on mixed values\n\t\t\t// unless filtered.\n\t\t\t( value ) => !! value && /\\d/.test( value )\n\t\t).length > 0\n\t);\n}\n\n/**\n * Extracts the unit of each side from the given box values.\n *\n * @param values Box values.\n *\n * @return Units for each side.\n */\nexport function getSelectedUnits( values?: BoxControlValue ): BoxControlValue {\n\treturn {\n\t\ttop: parseQuantityAndUnitFromRawValue( values?.top )[ 1 ],\n\t\tright: parseQuantityAndUnitFromRawValue( values?.right )[ 1 ],\n\t\tbottom: parseQuantityAndUnitFromRawValue( values?.bottom )[ 1 ],\n\t\tleft: parseQuantityAndUnitFromRawValue( values?.left )[ 1 ],\n\t};\n}\n\n/**\n * Normalizes provided sides configuration to an array containing only top,\n * right, bottom and left. This essentially just maps `horizontal` or `vertical`\n * to their appropriate sides to facilitate correctly determining value for\n * all input control.\n *\n * @param sides Available sides for box control.\n * @return Normalized sides configuration.\n */\nexport function normalizeSides( sides: BoxControlProps[ 'sides' ] ) {\n\tconst filteredSides: ( keyof BoxControlValue )[] = [];\n\n\tif ( ! sides?.length ) {\n\t\treturn ALL_SIDES;\n\t}\n\n\tif ( sides.includes( 'vertical' ) ) {\n\t\tfilteredSides.push( ...( [ 'top', 'bottom' ] as const ) );\n\t} else if ( sides.includes( 'horizontal' ) ) {\n\t\tfilteredSides.push( ...( [ 'left', 'right' ] as const ) );\n\t} else {\n\t\tconst newSides = ALL_SIDES.filter( ( side ) => sides.includes( side ) );\n\t\tfilteredSides.push( ...newSides );\n\t}\n\n\treturn filteredSides;\n}\n\n/**\n * Return the allowed sides based on the sides configuration.\n *\n * @param sides Sides configuration.\n * @return Allowed sides.\n */\nexport function getAllowedSides(\n\tsides: BoxControlInputControlProps[ 'sides' ]\n) {\n\tconst allowedSides: Set< keyof BoxControlValue > = new Set(\n\t\t! sides ? ALL_SIDES : []\n\t);\n\tsides?.forEach( ( allowedSide ) => {\n\t\tif ( allowedSide === 'vertical' ) {\n\t\t\tallowedSides.add( 'top' );\n\t\t\tallowedSides.add( 'bottom' );\n\t\t} else if ( allowedSide === 'horizontal' ) {\n\t\t\tallowedSides.add( 'right' );\n\t\t\tallowedSides.add( 'left' );\n\t\t} else {\n\t\t\tallowedSides.add( allowedSide );\n\t\t}\n\t} );\n\treturn allowedSides;\n}\n\n/**\n * Checks if a value is a preset value.\n *\n * @param value     The value to check.\n * @param presetKey The preset key to check against.\n * @return Whether the value is a preset value.\n */\nexport function isValuePreset( value: string, presetKey: string ) {\n\treturn value.startsWith( `var:preset|${ presetKey }|` );\n}\n\n/**\n * Returns the index of the preset value in the presets array.\n *\n * @param value     The value to check.\n * @param presetKey The preset key to check against.\n * @param presets   The array of presets to search.\n * @return The index of the preset value in the presets array.\n */\nexport function getPresetIndexFromValue(\n\tvalue: string,\n\tpresetKey: string,\n\tpresets: Preset[]\n) {\n\tif ( ! isValuePreset( value, presetKey ) ) {\n\t\treturn undefined;\n\t}\n\n\tconst match = value.match(\n\t\tnew RegExp( `^var:preset\\\\|${ presetKey }\\\\|(.+)$` )\n\t);\n\tif ( ! match ) {\n\t\treturn undefined;\n\t}\n\tconst slug = match[ 1 ];\n\tconst index = presets.findIndex( ( preset ) => {\n\t\treturn preset.slug === slug;\n\t} );\n\n\treturn index !== -1 ? index : undefined;\n}\n\n/**\n * Returns the preset value from the index.\n *\n * @param index     The index of the preset value in the presets array.\n * @param presetKey The preset key to check against.\n * @param presets   The array of presets to search.\n * @return The preset value from the index.\n */\nexport function getPresetValueFromIndex(\n\tindex: number,\n\tpresetKey: string,\n\tpresets: Preset[]\n) {\n\tconst preset = presets[ index ];\n\treturn `var:preset|${ presetKey }|${ preset.slug }`;\n}\n"],"mappings":";;;AAUA,MAAa,wBAA0C;CACtD,IAAI;EAAE,KAAK;EAAK,MAAM;CAAE;CACxB,KAAK;EAAE,KAAK;EAAK,MAAM;CAAE;CACzB,IAAI;EAAE,KAAK;EAAK,MAAM;CAAE;CACxB,IAAI;EAAE,KAAK;EAAK,MAAM;CAAE;CACxB,IAAI;EAAE,KAAK;EAAI,MAAM;CAAI;CACzB,IAAI;EAAE,KAAK;EAAI,MAAM;CAAI;CACzB,KAAK;EAAE,KAAK;EAAK,MAAM;CAAE;CACzB,KAAK;EAAE,KAAK;EAAK,MAAM;CAAE;CACzB,KAAK;EAAE,KAAK;EAAK,MAAM;CAAE;CACzB,KAAK;EAAE,KAAK;EAAK,MAAM;CAAE;CACzB,KAAK;EAAE,KAAK;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