import { analysis, getURLSearchParams } from '@/tools';

export * from './register.model';
export * from './analysis.model';
export * from './questionnaire.model';
export * from './report.model';
export * from './scheme.model';

export function application(data: Record<string, any>) {
  const image = analysis(
    [
      {key: 'preset', pattern: Number, default: 1},
      {key: 'el', pattern: String, multiple: true },
      {key: 'page', pattern: String},
      {key: 'com', pattern: String},
      {key: 'title', pattern: String},
      {key: 'debug', pattern: Boolean},
    ] as const,
    [data?.tabletDrainageImage, getURLSearchParams().get('image')].filter(Boolean).map(decodeURIComponent).join('&')
  );

  if (image.preset === 1) {
    if (!image.el) image.el = ['btn'];
  } else if (image.preset === 2) {
    if (!image.el) image.el = ['scan'];
    image.com ??= `scan.page`
    image.title ??= `萧山区中医智能辅诊系统`
  }
  image.el ??= [];
  image.debug ??= image.el.includes('debug');

  return {
    copyright: [data?.partner, data?.technicalSupporter].filter(Boolean).join('<br>'),
    registerFields: Array.isArray(data?.tabletFileFields) ? data.tabletFileFields : [],
    flowConfig: Array.isArray(data?.tabletProcessModules) ? data?.tabletProcessModules : [],
    pageElements: Array.isArray(data?.tabletRequiredPageOperationElements) ? data?.tabletRequiredPageOperationElements : [],
    image,
  };
}

export type ApplicationConfig = ReturnType<typeof application>;