"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),r=require("react");function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var o,a={exports:{}}; /*! Copyright (c) 2018 Jed Watson. Licensed under the MIT License (MIT), see http://jedwatson.github.io/classnames */o=a,function(){var e={}.hasOwnProperty;function r(){for(var t=[],o=0;o0===r.length?e:s(e[r[0]],r.slice(1)),l=(e,r,t)=>{const o=1===r.length?t:l(e[r[0]],r.slice(1),t);if(Array.isArray(e)){const t=[...e];return t.splice(r[0],1,o),t}return Object.assign(Object.assign({},e),{[r[0]]:o})};var i="formGenerator-module_formContainer__xRCk9",c="formGenerator-module_objectHeading__ZUwtz",m="formGenerator-module_removeButton__F1oYG",d="formGenerator-module_headerContainer__cyAQK",u="formGenerator-module_nestedMargin__pO4Su",f="formGenerator-module_objectContainer__URCvq",h="formGenerator-module_generatorError__heEWw",p="formGenerator-module_editorWrapper__00mPq";!function(e,r){void 0===r&&(r={});var t=r.insertAt;if(e&&"undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],a=document.createElement("style");a.type="text/css","top"===t&&o.firstChild?o.insertBefore(a,o.firstChild):o.appendChild(a),a.styleSheet?a.styleSheet.cssText=e:a.appendChild(document.createTextNode(e))}}(".formGenerator-module_formContainer__xRCk9{width:100%}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_objectHeading__ZUwtz{font-size:1.25em;font-weight:600;margin:0}.formGenerator-module_formContainer__xRCk9 label{margin-bottom:.5rem}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_removeButton__F1oYG:disabled{cursor:not-allowed!important;pointer-events:all!important}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_removeButton__F1oYG:disabled:active{background-color:#dc3545!important;border-color:#dc3545!important}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_removeButton__F1oYG svg{margin-bottom:.2em;scale:1.1}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_headerContainer__cyAQK{align-items:center;display:flex;flex-direction:row;height:2.38rem;justify-content:space-between;margin-bottom:1em;margin-top:1.5em;width:100%}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_headerContainer__cyAQK.formGenerator-module_nestedMargin__pO4Su:first-child{margin-top:.25em}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_objectContainer__URCvq h1{scroll-margin-top:6em}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_objectContainer__URCvq.form-control{margin-top:1.75em}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_generatorError__heEWw{background-color:#f8d7da;border-radius:.5em;color:#721c24;font-weight:600;margin:2rem 0;padding:1em;text-align:center;width:100%}.formGenerator-module_formContainer__xRCk9 .formGenerator-module_editorWrapper__00mPq{background-color:#1e1e1e;border-radius:.375rem;padding:.75rem 0}");const _={isTextArea:(e,r)=>"$useTextArea"===e,isDateArea:(e,r)=>"$useDateArea"===e,isCodeArea:(e,r)=>"$useCodeArea"===e,isSelectOptions:(e,r)=>"$useSelectOptions"===e},{isTextArea:g,isDateArea:b,isSelectOptions:j,isCodeArea:v}=_,y=e=>!!Object.entries(_).find((([r,t])=>t(e,{}))),x=(e,r)=>y(e);exports.default=t=>{var o,a;const[_,C]=r.useState(t.seed),[O,G]=r.useState({}),k=!t.suppressStyles&&t.floatingLabels,N=!t.suppressStyles&&t.groupNestedObjects,A=r.useRef({}),w=r.useCallback((e=>{let r={};const t=(e,o,a)=>{for(const[n,s]of Object.entries(e)){const e=[...o,n];if(a){if(g(a,s)||b(a,s))r=l(r,e,s);else if(v(a,s))r=l(r,e,s._value);else if(j(a,s)){const e=F(s);t(Object.assign({[n]:s._defaultValue},e.reduce(((e,r)=>Object.assign(Object.assign({},e),r[1])),{})),o)}}else y(n)&&x(n)?t(s,o,n):Array.isArray(s)?s.length>0&&"string"==typeof s[0]?r=l(r,e,s):(r=l(r,e,[]),s.map(((r,o)=>t(r,[...e,o])))):"object"==typeof s?t(s,e):r=l(r,e,"string"==typeof s||"number"==typeof s||"boolean"==typeof s?s:{ParseError:"unhandled payload type encountered"})}};return t(e,[]),G(r),r}),[]);r.useEffect((()=>{w(_)}),[w,_]);const{onChange:S}=t;r.useEffect((()=>{null==S||S(O)}),[S,O]);const R=e=>n({[e]:!t.suppressStyles}),P=e=>R(n(e,{"form-floating":k})),F=(e,r)=>{const t=e._defaultValue,o=[];if(Array.isArray(t))for(const[r,a]of e._options.entries()){const n=e._options[r]._assocPayload;t.includes(a._value)&&n&&o.push([r,n])}else{const t=e._options.findIndex((r=>r._value===e._defaultValue));if(-1===t){const[t]=e._options;r&&r(t._value),t._assocPayload&&o.push([0,t._assocPayload])}else{const r=e._options[t]._assocPayload;r&&o.push([t,r])}}return o},E=o=>{const{seed:a,keychain:i,keyword:_}=o,O=[];for(const[o,G]of Object.entries(a)){const a=[...i,o],w=a.join("-").replaceAll(" ","-"),S=a.join("."),M=(r,t)=>e.jsx("label",Object.assign({className:r?R(r):"",htmlFor:w},{children:null!=t?t:o})),V=e=>{C((r=>l(r,a,[...s(r,a),e])))},$=e=>{C((r=>l(r,a,s(r,a).filter(((r,t)=>t!==e)))))},q=(e,r)=>{C((t=>l(t,null!=r?r:a,e)))},B=e=>{C((r=>l(r,[...a,"_defaultValue"],e)))};try{if(_){if(g(_,G))O.push(e.jsxs("div",Object.assign({className:P("mb-3")},{children:[!k&&M(),e.jsx("textarea",{id:w,className:R("form-control"),value:G,onChange:e=>q(e.target.value),placeholder:k?"placeholder":""}),k&&M()]}),S));else if(b(_,G))O.push(e.jsxs("div",Object.assign({className:P("mb-3")},{children:[!k&&M(),e.jsx("input",{id:w,className:R("form-control"),value:G,type:"date",onChange:e=>q(e.target.value),placeholder:k?"placeholder":""}),k&&M()]}),S));else if(v(_,G))O.push(e.jsxs(r.Fragment,{children:[M(),e.jsx("div",Object.assign({className:R(n("mb-3",p))},{children:e.jsx(G._instance,{height:"13em",language:G._language,theme:"vs-dark",value:G._value,onChange:e=>q(e,[...a,"_value"]),options:{minimap:{enabled:!1},lineDecorationsWidth:0,lineNumbersMinChars:3,automaticLayout:!0}})}))]},S));else if(j(_,G)){const r=F(G,B),t=Array.isArray(G._defaultValue);O.push(e.jsxs("div",Object.assign({className:R(n({[n("mb-4 form-control",f)]:N&&G._options.find((e=>!!e._assocPayload))}))},{children:[e.jsxs("div",Object.assign({className:t?"":P()},{children:[(!k||t)&&M(),e.jsx("select",Object.assign({id:w,className:R("form-select mb-3"),value:G._defaultValue,onChange:e=>{let r;if(t){const t=[];for(let r=0;r{var o;return e.jsx("option",Object.assign({value:r._value},{children:null!==(o=r._label)&&void 0!==o?o:r._value}),t)}))})),k&&!t&&M()]})),r.map((([e,r])=>E({seed:r,keychain:[...a,"_options",e,"_assocPayload"]})))]}),S))}}else if(y(o)&&x(o))O.push(e.jsx(r.Fragment,{children:E({seed:G,keychain:a,keyword:o})},S));else if(Array.isArray(G))O.push(e.jsxs("div",Object.assign({className:R(n("mb-4",f,{"form-control":N}))},{children:[G.map(((s,l)=>{const i=`${o}[${l}]`;return A.current[S]=[],e.jsxs(r.Fragment,{children:[e.jsxs("div",Object.assign({className:n(d,{[u]:N})},{children:[e.jsx("div",Object.assign({className:c,ref:e=>{e&&A.current[S].push(e)}},{children:i})),e.jsx("button",Object.assign({className:n(R("btn btn-danger"),m),type:"button",onClick:()=>$(l),disabled:G.length<=1,title:`Remove '${i}'`},{children:t.suppressStyles?"Remove":e.jsx("svg",Object.assign({xmlns:"http://www.w3.org/2000/svg",width:"16",height:"16",fill:"currentColor",className:"bi bi-trash3-fill",viewBox:"0 0 16 16"},{children:e.jsx("path",{d:"M11 1.5v1h3.5a.5.5 0 0 1 0 1h-.538l-.853 10.66A2 2 0 0 1 11.115 16h-6.23a2 2 0 0 1-1.994-1.84L2.038 3.5H1.5a.5.5 0 0 1 0-1H5v-1A1.5 1.5 0 0 1 6.5 0h3A1.5 1.5 0 0 1 11 1.5Zm-5 0v1h4v-1a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5ZM4.5 5.029l.5 8.5a.5.5 0 1 0 .998-.06l-.5-8.5a.5.5 0 1 0-.998.06Zm6.53-.528a.5.5 0 0 0-.528.47l-.5 8.5a.5.5 0 0 0 .998.058l.5-8.5a.5.5 0 0 0-.47-.528ZM8 4.5a.5.5 0 0 0-.5.5v8.5a.5.5 0 0 0 1 0V5a.5.5 0 0 0-.5-.5Z"})}))}))]})),E({seed:G[l],keychain:[...a,l]})]},l)})),e.jsx("button",Object.assign({className:n("mt-2 mb-3",R("btn btn-secondary")),type:"button",onClick:()=>{var e;V(s(t.seed,[...a,0])),console.log(A.current[S][A.current[S].length-1]),null===(e=A.current[S][A.current[S].length-1])||void 0===e||e.scrollIntoView()}},{children:`Add '${o}'`})),!N&&e.jsx("hr",{className:"mt-3 mb-4"})]}),S));else if("object"==typeof G)O.push(e.jsxs("div",Object.assign({className:R(n("mb-4",f,{"form-control":N}))},{children:[e.jsx("div",Object.assign({className:n(d,{[u]:N})},{children:e.jsx("div",Object.assign({className:c},{children:o}))})),E({seed:G,keychain:a})]}),S));else if("string"==typeof G||"number"==typeof G)O.push(e.jsxs("div",Object.assign({className:P("mb-3")},{children:[!k&&M(),e.jsx("input",Object.assign({id:w,className:R("form-control"),value:G,placeholder:k?"placeholder":""},"number"==typeof G?{type:"number",onChange:e=>q(Number(e.target.value))}:{type:"text",onChange:e=>q(e.target.value)})),k&&M()]}),S));else{if("boolean"!=typeof G)throw"Unhandled payload type: "+typeof G;O.push(e.jsxs("div",Object.assign({className:R("mt-4 mb-3 form-check")},{children:[e.jsx("input",{id:w,className:R("form-check-input"),type:"checkbox",checked:G,onChange:e=>q(e.target.checked)}),M("form-check-label")]}),S))}}catch(r){console.error(String(r)),O.push(e.jsx("div",Object.assign({className:h},{children:"Form Generator Error: see console"}),S))}}return e.jsx(e.Fragment,{children:O})};return e.jsx("div",Object.assign({className:i},{children:e.jsxs("form",Object.assign({id:null!==(o=t.formId)&&void 0!==o?o:"form-generator",onSubmit:e=>{var r;e.preventDefault(),null===(r=t.onSubmit)||void 0===r||r.call(t,O)}},{children:[E({seed:_,keychain:[]}),t.onSubmit&&e.jsxs(e.Fragment,{children:[e.jsx("hr",{className:R("mt-4")}),e.jsx("button",Object.assign({className:R("btn btn-primary"),form:null!==(a=t.formId)&&void 0!==a?a:"form-generator",type:"submit"},{children:"Submit"}))]})]}))}))}; //# sourceMappingURL=index.cjs.map