import { AttachmentApi, DocumentApi } from '@openapi/document-registry';
import store from '@state/store';
import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get';
import set from 'lodash/set';
import axios, { AxiosResponse } from 'axios';

const attachmentApi = new AttachmentApi();
const documentApi = new DocumentApi();
const columnsBase = {
  create: [
    'regions',
    'organizations',
    'accounts',
    'comment',
    'range',
    // "startDate",
    // "last",
    'every',
  ],
  search: [
    'regions',
    'organizations',
    'accounts',
    'comment',
    'range',
    // "startDate",
    // "last",
    'every',
  ],
  card: [
    'name',
    'services',
    'region',
    'organization',
    'account',
    'startDate',
    'period',
    'comment',
    'download',
  ],
  reports: [
    'id',
    'regions',
    'organizations',
    'startDate',
    'every',
    'comment',
    'download',
  ],
  reportsAuto: [
    'id',
    'regions',
    'organizations',
    'startDate',
    'every',
    'comment',
    'download',
  ],
  attachments: [
    // 'id',
    'date',
    'range',
    'download',
  ],
};

const mock = [
  {
    id: '1',
    name: 'Отчет по количеству принятых заявлений в разрезе услуг',
    columns: {
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
          },
        },
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            hidden: true
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',   
      ],
      reports: ['id', 'regions', 'organizations', 'comment', 'download'],
      reportsAuto: [
        'id',
        'regions',
        'organizations',

        // 'last',
        'comment',

        'every',
        'downloadAll',
      ],
    },
  },
  {
    id: '2',
    name: 'Детализированный отчет по заявлениям в разрезе услуг',  
    columns: {
      ...columnsBase,
      create: [
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
          },
        },
        {
          name: 'organizations',
          props: {
            multiple: false,
            clearable: true,
            required: true
          },
        }, 
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],      
      observerRFCreate: [
        'id',
        {
          name: 'regions',
          props: {
            multiple: false,
            clearable: true,
          }
        }, 
        'organizations',  
               
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        'id',
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',          
          props: {
            required: true 
          }
        },              
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        'id',
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: [
        'id',
        'regions',
        'organizations',
        'accounts',
        'comment',
        'download',
      ],
      reportsAuto: [
        'id',
        'regions',
        'organizations',
        'accounts',

        // "last",
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {
    id: '3',
    name: 'Отчет по услугам в разрезе статуса',
    columns: {
      ...columnsBase,
      create: [
        'id',
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },        
        {
          name: 'services',
          props: {
            required: true,
          }
        },        
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        'id',
        {
          name: 'regions',
          props: {
            required: false,
            multiple: false,
            clearable: true,
          }
        }, 
        'organizations',      
        {
          name: 'services',
          props: {
            required: true
          }
        },        
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        'id',
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            required: true 
          }
        },        
        {
          name: 'services',
          props: {
            required: true
          }
        },        
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        'id',
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',         
          props: {
            hidden: true,
          }
        },        
        {
          name: 'services',
          props: {
            required: true
          }
        },        
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'services', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  // {
  //   id: '20',
  //   name: 'Сводный отчет по услугам в динамике по месяцам',
  //   scope: ['RFObserverScope', 'fedStatObserver'],
  //   columns: {
  //     ...columnsBase,
  //     create: [
  //       {
  //         name: 'regions',
  //         props: {
  //           required: false,
  //         },
  //       },
  //       {
  //         name: 'organizations',
  //         props: {
  //           hidden: false,
  //         },
  //       },
  //       {
  //         name: 'services',
  //         props: {
  //           hidden: true,
  //         },
  //       }, 
  //       'comment',
  //       'range',
  //       // "startDate",
  //       // "last",
  //       'every',
  //     ],
  //     observerRFCreate: [
  //       'id',
  //       {
  //         name: 'regions',
  //         props: {
  //           required: false,
  //         },
  //       },
  //       'organizations',
  //       {
  //         name: 'services',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //       'comment',
  //       'range',
  //       // "startDate",
  //       // "last",
  //       'every',
  //     ],
  //     observerRegionCreate: [
  //       'id',
  //       {
  //         name: 'regions',
  //         props: {
  //           hidden: true,
  //         },
  //       },
  //       'organizations',
  //       {
  //         name: 'services',
  //         props: {
  //           required: false,
  //           hidden: true,
  //         },
  //       },
  //       'comment',
  //       'range',
  //       // "startDate",
  //       // "last",
  //       'every',
  //     ],
  //     observerOrganizationCreate: [
  //       'id',
  //       {
  //         name: 'regions',
  //         props: {
  //           hidden: true,
  //         }
  //       }, 
  //       {
  //         name: 'organizations',
  //         props: {
  //           hidden: true,
  //         }
  //       },
  //       'comment',
  //       'range',
  //       // "startDate",
  //       // "last",
  //       'every',
  //     ],
  //     reports: ['id', 'range', 'comment', 'download'],
  //     reportsAuto: [
  //       'id',
  //       // "last",
  //       'every',
  //       'comment',
  //       'downloadAll',
  //     ],
  //   },
  // },
  {
    id: '10',
    name: 'Отчет по отказам в разрезе услуг и заявлений',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',
          props: {
            hidden: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },  
  {  
    id: '14',
    name: 'Детализированный отчет по отказам в разрезе причин',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {
    id: '9',
    name: 'Сводный отчет по отказам',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',         
          props: {
            hidden: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },  
  {
    id: '6',
    name: 'Отчет по нарушениям срока регистрации в разрезе пользователя',
    columns: {
      ...columnsBase,
      create: [
        // 'services',
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        // 'services',
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            hidden: true
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',   
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {  
    id: '17',
    name: 'Детализированный отчет о соблюдении срока регистрации',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        'regions',
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {
    id: '7',
    name: 'Отчет по нарушениям срока предоставления в разрезе услуг',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        // 'services',
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            hidden: true
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',   
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'every',
        'comment',
        'downloadAll',
      ]
    }
  }, 
  {
    id: '15',
    name: 'Детализированный отчет о соблюдении срока предоставления услуги',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',
          props: {
            hidden: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {  
    id: '16',
    name: 'Сводный отчет по услугам с нарушением срока предоставления',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {  
    id: '18',
    name: 'Сводный отчет по услуге в разрезе статусов заявителей',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },  
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {  
    id: '11',
    name: 'Отчет по исполнению сроков административных процедур',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {
    id: '12',
    name: 'Отчет по исполнению сроков межведомственного взаимодействия',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        // 'services',
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            hidden: true
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',   
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'every',
        'comment',
        'downloadAll',
      ]
    }
  }, 
  {
    id: '19',
    name: 'Детализированный отчет по межведомственному взаимодействию',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        'regions',
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        'organizations',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        },
        {
          name: 'organizations',
          props: {
            hidden: true
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',   
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
  {  
    id: '13',
    name: 'Агрегированный отчет по услугам и межведомственному взаимодействию',
    columns: {
      ...columnsBase,
      create: [
        'regions',
        {
          name: 'organizations',
          props: {
            required: true,
          }
        },
        {
          name: 'services',
          props: {
            required: true,
          }
        },
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRFCreate: [
        {
          name: 'regions',
          props: {
            required: false,
          }
        }, 
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerRegionCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        'organizations',
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      observerOrganizationCreate: [
        {
          name: 'regions',
          props: {
            hidden: true,
          }
        }, 
        {
          name: 'organizations',        
          props: {
            hidden: true,
          }
        },
        'services',
        'comment',
        'range',
        // "startDate",
        // "last",
        'every',
      ],
      reports: ['id', 'range', 'comment', 'download'],
      reportsAuto: [
        'id',
        'services',
        // 'last',
        'every',
        'comment',
        'downloadAll',
      ],
    },
  },
];

function fetchAttachment(
  schema: string,
  id: string,
  fileName: string,
  urlOnly = true
) {
  return attachmentApi
    .getDocumentFile(schema, id, fileName)
    .then((result) => {
      return urlOnly ? result.request?.responseURL : result.data;
    })
    .catch((e) => e);
}

function fetchAttachmentReport(
  schema: string,
  id: string,
  fileName: string
): Promise<AttachmentReport> {
  return attachmentApi
    .getDocumentFile(schema, id, fileName, { responseType: "blob" })
    .then((result: AxiosResponse) => {
      return {
        presignedUrl: result.request?.responseURL,
        file: new Blob([result.data]),
      };
    })
    .catch((e) => e);
}

function fetchBaseReport(uuid: string) {
  return new Promise((resolve) => {
    return resolve(cloneDeep(mock.find(({ id }) => id === uuid)));
  });
}

function fetchBaseReports() {
  return new Promise((resolve) => {
    return resolve(cloneDeep(mock));
  });
}

function fetchReportsByParams({autoEnabled, reportId, creatorId, parentIds_in, parentIds_notin, number_like, page, perPage}: ReportsList) {
  if (typeof reportId !== 'undefined') {
    return axios.get(
      `/service/document/registry/statreports?reportId=${reportId}`
    )
  } else {
    return axios.get(
      '/service/document/registry/statreports/paging?' +
      'creatorId=' + creatorId +
      (parentIds_notin ? '&parentIds_notin=' + parentIds_notin : '') +
      (parentIds_in ? '&parentIds_in=' + parentIds_in : '') +
      (number_like ? '&number_like=' + number_like  : '') +
      (page ? '&page=' + page  : '') +
      (perPage ? '&perPage=' + perPage  : '&perPage=10') +
      '&autoEnabled=' + autoEnabled
    )
  }
}

function dispatchStatement(id: string, opts: any, expand?: string) {
  const defaultExpand =
    'applicant,delegate,history,documents,interDepartmentReq';
  const currentExpand = expand ? `${defaultExpand},${expand}` : defaultExpand;
  return documentApi
    .getDocument('Applications', id, currentExpand, opts)
    .then(async ({ data: response }: any) => {
      const statementsModel = response;

      statementsModel.dictionaries = {
        external: {
          dictionary: 'gpzu_1_status',
          name: 'name',
        },
        internal: {
          dictionary: 'gpzu_2_application_status',
          name: 'description',
        },
        typeRecipient: 'gpzu_12_contract_type',
        userType: 'gpzu_3_subject_type',
        document: 'gpzu_14_identity_document',
        gender: 'gpzu_15_sex_type',
      };

      let documents = get(statementsModel, 'documents') || [];

      const service = await store.dispatch(
        'registryService/services/getServiceMeta',
        {
          id: get(statementsModel, 'system.serviceId'),
          opts: {
            expand: ['interagencies.formRequest', 'interagencies.formResponse'],
          },
        },
        { root: true }
      );

      const serviceDocs = (service.documents || []).filter((doc: any) => {
        return documents.find(
          (statementDoc: any) =>
            get(statementDoc, 'type.code') === get(doc, 'code')
        );
      });

      documents = documents.map((el: any) => {
        const serviceDoc = serviceDocs.find(
          (serviceDoc: any) => serviceDoc.code === el.type.code
        );

        return {
          ...el,
          type: {
            ...el.type,
            name: el.type.name || serviceDoc?.name,
          },
        };
      });

      set(statementsModel, 'documents', documents);

      return statementsModel;
    });
}

declare interface AttachmentReport {
  presignedUrl?: string | undefined;
  file: Blob;
}

export type ReportsList ={
  autoEnabled?: boolean;
  creatorId?: string; 
  reportId?:string; 
  parentIds_in?: string; 
  parentIds_notin?: string;
  number_like?: string;
  page?: number;
  perPage?: number;
}

export {
  fetchAttachment,
  fetchAttachmentReport,
  fetchBaseReport,
  fetchBaseReports,
  dispatchStatement,
  fetchReportsByParams,
};
