import type { IFieldBase } from "$lib/Models"; import { FormModel, // ImageModel, // FileModel, // ListModel, // MapModel, SelectModel, TextModel, NumberModel, ChoiceModel, // CalendarModel, // CsvModel } from '../Models'; // experiment import NumberVisual from '../Components/NumberVisual.svelte'; // end // *** TODO --> build a validation step based on the YAAS api validation pattern that validates a users input. export const buildFields = (focus: { [key: string]: any }, parentId: string): { [key:string]: IFieldBase } => { return Object.keys(focus).reduce((acc, name) => { let field; let type; let value = focus[name]; console.log("shit i got a headrush: ", name, value, field); switch(typeof focus[name]) { case 'string': { type = 'text'; console.log("leavel leavlel : ", name, TextModel); field = new TextModel({ type, name, value, parentId, // TODO: all validation added here is for testing only validation: [ { operator: 'required', anchorValues: null, message: 'we can try' } ] }); break; } case 'number': { if(focus[name].type === 'range'){ type = 'range'; } else { type = 'number'; } field = new NumberModel({ type, name, value, parentId, subType: 'custom', customVisual: NumberVisual, waitForHowManyMSBeforeSubmit: 5000, // TODO: all validation added here is for testing only validation: [ { operator: 'greaterThan', anchorValues: [4], message: 'we can try' } ] }); break; } case 'boolean': { type = 'choice'; field = new ChoiceModel({ parentId, subType: 'radio', type, name, value: [], dataKey: 'name', data: [ { name: 'right', quality: true }, { name: 'wrong', quality: false } ], // TODO: all validation added here is for testing only validation: [ { operator: 'required', anchorValues: null, message: 'we can try' } ] }); break; } case 'object': { if (!!focus[name] && focus[name].type === 'checkbox' || Array.isArray(focus[name])) { type = 'choice'; field = new ChoiceModel({ parentId, type, subType: 'checkbox', name, value: [], dataKey: '', data: focus[name], // TODO: all validation added here is for testing only validation: [ { operator: 'chosenAtLeast', anchorValues: [3], message: 'we can try' } ] }); break; } else if (!!focus[name] && focus[name].type === 'calendar') { type = 'calendar'; // field = new CalendarModel({ // parentId, // subType: 'regular', // type, // name, // value: {}, // // data: [ // // { name: 'right', quality: true }, // // { name: 'wrong', quality: false } // // ], // // TODO: all validation added here is for testing only // validation: [ { operator: 'required', anchorValues: null, message: 'we can try' } ] // }); break; } else if(!!focus[name] && focus[name].type === 'image'){ type = 'file'; // field = new ImageModel({ type, name, value, parentId }); } else if(!!focus[name] && focus[name].type === 'csv'){ type = 'csv'; // field = new CsvModel({ // type, // parentId, // name, // value, // }); } else if(!!focus[name] && focus[name].type === 'file'){ type = 'file'; // field = new FileModel({ type, name, value, parentId }); } else if (!!focus[name] && focus[name].type === 'select') { field = new SelectModel({ parentId, type, name, value, endpoint: focus[name].endpoint, // TODO: all validation added here is for testing only validation: [ { operator: 'required', anchorValues: null, message: 'we can try' } ] }); break; } else if (!!focus[name] && focus[name].type === 'map') { // field = new MapModel({ // parentId, // type, // name, // value, // ...focus[name] // }); break; } else if (!!focus[name] && focus[name].type === 'list' || ( Array.isArray(focus[name]) && (focus[name] as any[]).some(d => typeof d === 'object') ) ) { // field = new ListModel({ // parentId, // type, // name, // value, // ...focus[name] // }); break; } else { type = 'form'; field = new FormModel({ type, name, value, parentId, autosubmit: true }); value = buildFields( focus[name], field.id ); field.value = value; } break; } default: { } }; return { ...acc, [field.id]: field }; }, {} as { [key:string]: IFieldBase }); };