{"version":3,"file":"active-form.mjs","names":[],"sources":["../../../../../../react-form/src/active-form.ts"],"sourcesContent":["import { ActiveForms, FormInterface } from \"./types\";\r\n\r\n// Current active form\r\nlet activeForm: FormInterface | null = null;\r\n\r\n// Active forms ids\r\nconst activeFormsIds: string[] = [];\r\n\r\n/**\r\n * List of all active forms\r\n */\r\nconst activeForms: ActiveForms = {};\r\n\r\n/**\r\n * Get the current active form\r\n */\r\nexport function getActiveForm(): FormInterface | null {\r\n  return activeForm;\r\n}\r\n\r\n/**\r\n * Set the current active form\r\n */\r\nexport function setActiveForm(form: FormInterface): void {\r\n  activeForm = form;\r\n  if (!activeFormsIds.includes(form.id)) {\r\n    activeFormsIds.push(form.id);\r\n  }\r\n}\r\n\r\n/**\r\n * Remove the given form from being active form.\r\n *\r\n * The id is purged from `activeFormsIds` **unconditionally** — not only when the\r\n * form is the current active one — otherwise a form unmounted while a different\r\n * form is active would leave a dead id behind. Over a long-lived app that\r\n * mounts/unmounts forms out of LIFO order (modals, routes, wizards) those dead\r\n * ids would accumulate and the \"restore last active form\" logic would resolve\r\n * stale ids and fail to restore a still-live form.\r\n */\r\nexport function removeActiveForm(form: FormInterface): void {\r\n  const index = activeFormsIds.indexOf(form.id);\r\n  if (index > -1) {\r\n    activeFormsIds.splice(index, 1);\r\n  }\r\n\r\n  // Only re-pick an active form when the one being removed WAS active.\r\n  if (activeForm && form.id === activeForm.id) {\r\n    activeForm = null;\r\n\r\n    // Walk from the most recent, skipping any id whose form is already gone.\r\n    for (let i = activeFormsIds.length - 1; i >= 0; i--) {\r\n      const candidate = getForm(activeFormsIds[i]);\r\n\r\n      if (candidate) {\r\n        setActiveForm(candidate);\r\n        break;\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\n/**\r\n * Get form by the given id\r\n */\r\nexport function getForm(formId: string): FormInterface | null {\r\n  return activeForms[formId] || null;\r\n}\r\n\r\n/**\r\n * Add the given form to the active forms\r\n */\r\nexport function addToFormsList(form: FormInterface): void {\r\n  activeForms[form.id] = form;\r\n}\r\n\r\n/**\r\n * Remove the given form from the active forms\r\n */\r\nexport function removeFromFormsList(form: FormInterface): void {\r\n  delete activeForms[form.id];\r\n}\r\n"],"mappings":";AAGA,IAAI,aAAmC;AAGvC,MAAM,iBAA2B,CAAC;;;;AAKlC,MAAM,cAA2B,CAAC;;;;AAKlC,SAAgB,gBAAsC;CACpD,OAAO;AACT;;;;AAKA,SAAgB,cAAc,MAA2B;CACvD,aAAa;CACb,IAAI,CAAC,eAAe,SAAS,KAAK,EAAE,GAClC,eAAe,KAAK,KAAK,EAAE;AAE/B;;;;;;;;;;;AAYA,SAAgB,iBAAiB,MAA2B;CAC1D,MAAM,QAAQ,eAAe,QAAQ,KAAK,EAAE;CAC5C,IAAI,QAAQ,IACV,eAAe,OAAO,OAAO,CAAC;CAIhC,IAAI,cAAc,KAAK,OAAO,WAAW,IAAI;EAC3C,aAAa;EAGb,KAAK,IAAI,IAAI,eAAe,SAAS,GAAG,KAAK,GAAG,KAAK;GACnD,MAAM,YAAY,QAAQ,eAAe,EAAE;GAE3C,IAAI,WAAW;IACb,cAAc,SAAS;IACvB;GACF;EACF;CACF;AACF;;;;AAKA,SAAgB,QAAQ,QAAsC;CAC5D,OAAO,YAAY,WAAW;AAChC;;;;AAKA,SAAgB,eAAe,MAA2B;CACxD,YAAY,KAAK,MAAM;AACzB;;;;AAKA,SAAgB,oBAAoB,MAA2B;CAC7D,OAAO,YAAY,KAAK;AAC1B"}