import { GetterTree, MutationTree } from 'vuex';

type FileModel = {
  id: string;
  type: {
    data: {
      type:'document' | 'sign',
      code: string
  }};
  file: File;
  typeDocument: string;
};

interface State {
  data: null | Record<string, any>;
  create?: {
    isClearModalOpen: boolean;
    steppedBack: boolean;
    foundPerson?: any;
    statementDocuments?: FileModel[];
  }
}

export const state: State = {
  data: null,
  create: {
    isClearModalOpen: false,
    steppedBack: false,
    foundPerson: null,
    statementDocuments: [],
  }
};

export const getters: GetterTree<State, State> = {
  data(state) {
    return state.data;
  },
  isClearModalOpen(state) {
    return state.create?.isClearModalOpen && state.create?.steppedBack;
  },
  steppedBack(state) {
    return state.create?.steppedBack;
  },
  foundPerson(state) {
    return state.create?.foundPerson;
  },
  statementDocuments(state) {
    return state.create?.statementDocuments;
  },
};

export const mutations: MutationTree<State> = {
  data(state, data) {
    state.data = { ...state.data, ...data };
  },
  setClearModal(state, data) {
    if (state.create) {
      if ((state.create.steppedBack) || (!state.create.steppedBack && !data)) {
        state.create.isClearModalOpen = data;
      }
    }
  },
  setSteppedBack(state, data) {
    if (state.create) {
      state.create.steppedBack = data;
    }
  },
  setFoundPerson(state, data) {
    if (state.create) {
      state.create.foundPerson = data;
    }
  },
  setStatementDocuments(state, data) {
    if (state.create) {
      state.create.statementDocuments = data;
    }
  },
};
