{"version":3,"file":"valueToNodeConfig.cjs","names":["flatMap","isString","getParents","getChildren","recomposeConfig","replaceAll"],"sources":["../../../src/states/functions/valueToNodeConfig.ts"],"sourcesContent":["import { DEFAULT_DELIMITER } from '#constants';\nimport { replaceAll } from '#utils';\nimport { decomposeSV, type StateValue } from '@bemedev/decompose';\nimport { isString } from '~types';\n\nimport type { NodeConfig } from '../types';\nimport { flatMap } from './flatMap';\nimport { getChildren } from './getChildren';\nimport { getParents } from './getParents';\nimport { recomposeConfig } from './recompose';\n\nexport type ValueToNodeConfig_F = <T extends StateValue>(\n  body: NodeConfig,\n  from: T,\n  initial?: boolean,\n) => NodeConfig;\n\n/**\n * Converts a state value to a node configuration based on the provided body and from value.\n *\n * @param body - The node configuration body to convert from.\n * @param from - The state value to convert to a node configuration.\n * @param initial - Optional flag to indicate if the initial state should be included.\n * @returns A node configuration object that represents the state value.\n *\n * @see {@linkcode ValueToNodeConfig_F} for more details\n * @see {@linkcode flatMap} for flattening the node configuration\n * @see {@linkcode getChildren} for retrieving child states\n * @see {@linkcode getParents} for retrieving parent states\n * @see {@linkcode recomposeConfig} for recomposing the node configuration\n * @see {@linkcode decomposeSV} for decomposing state values\n * @see {@linkcode replaceAll} for replacing substrings in the state value\n * @see {@linkcode DEFAULT_DELIMITER} for the default delimiter used in state paths\n */\nexport const valueToNodeConfig: ValueToNodeConfig_F = (body, from) => {\n  const flatBody = flatMap(body as NodeConfig, false);\n  const keysFlatBody = Object.keys(flatBody);\n  const fromIsString = isString(from);\n  if (fromIsString) {\n    const check2 = keysFlatBody.includes(from);\n    if (check2) {\n      const parents = getParents(from as any);\n      const children = getChildren(from, ...keysFlatBody);\n\n      const out1: any = {};\n\n      parents.concat(children).forEach(key => {\n        out1[key] = (flatBody as any)[key];\n      });\n\n      const out: any = recomposeConfig(out1);\n      return out;\n    }\n    return {};\n  }\n\n  const flatFrom = decomposeSV(from)\n    .map(key =>\n      replaceAll({\n        entry: key,\n        match: '.',\n        replacement: DEFAULT_DELIMITER,\n      }),\n    )\n    .map(key => `/${key}`);\n\n  const out1: any = {};\n\n  flatFrom.forEach((key1, _, all) => {\n    const check4 = keysFlatBody.some(key => key.startsWith(key1));\n\n    if (check4) {\n      out1[key1] = (flatBody as any)[key1];\n\n      const initial = (flatBody as any)[key1].initial;\n      if (initial) {\n        const _initial = `${key1}${DEFAULT_DELIMITER}${initial}`;\n        const cannotContinue = all.some(key =>\n          key.startsWith(`${key1}${DEFAULT_DELIMITER}`),\n        );\n        if (cannotContinue) return;\n        out1[_initial] = (flatBody as any)[_initial];\n      }\n    }\n  });\n\n  const out2 = recomposeConfig(out1);\n  return out2;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAa,qBAA0C,MAAM,SAAS;CACpE,MAAM,WAAWA,iCAAAA,QAAQ,MAAoB,MAAM;CACnD,MAAM,eAAe,OAAO,KAAK,SAAS;AAE1C,KADqBC,yBAAAA,SAAS,KAAK,EACjB;AAEhB,MADe,aAAa,SAAS,KAAK,EAC9B;GACV,MAAM,UAAUC,oCAAAA,WAAW,KAAY;GACvC,MAAM,WAAWC,qCAAAA,YAAY,MAAM,GAAG,aAAa;GAEnD,MAAM,OAAY,EAAE;AAEpB,WAAQ,OAAO,SAAS,CAAC,SAAQ,QAAO;AACtC,SAAK,OAAQ,SAAiB;KAC9B;AAGF,UADiBC,mCAAAA,gBAAgB,KAAK;;AAGxC,SAAO,EAAE;;CAGX,MAAM,YAAA,GAAA,mBAAA,aAAuB,KAAK,CAC/B,KAAI,QACHC,iCAAAA,WAAW;EACT,OAAO;EACP,OAAO;EACP,aAAA;EACD,CAAC,CACH,CACA,KAAI,QAAO,IAAI,MAAM;CAExB,MAAM,OAAY,EAAE;AAEpB,UAAS,SAAS,MAAM,GAAG,QAAQ;AAGjC,MAFe,aAAa,MAAK,QAAO,IAAI,WAAW,KAAK,CAAC,EAEjD;AACV,QAAK,QAAS,SAAiB;GAE/B,MAAM,UAAW,SAAiB,MAAM;AACxC,OAAI,SAAS;IACX,MAAM,WAAW,GAAG,QAA2B;AAI/C,QAHuB,IAAI,MAAK,QAC9B,IAAI,WAAW,GAAG,QAA2B,CAC9C,CACmB;AACpB,SAAK,YAAa,SAAiB;;;GAGvC;AAGF,QADaD,mCAAAA,gBAAgB,KAAK"}