import { getMockData } from 'mock/allMockData';

const baseApiUrl = '/api/';

const allData: any[] = [];
let feteched = false;

const fetchData = () => {
  if (!feteched) {
    allData.push(...getMockData('jhi_user'));
    feteched = true;
  }
};

export function setAuthMock(mock) {
  mock
    ?.onPost(`${baseApiUrl}authenticate/withoutCaptcha`)
    .reply((config) => {
      console.log('withoutCaptcha.config', config);
      const { username = '', password = '' } = JSON.parse(config.data);
      // return an array in the form of [status, data, headers]
      if (username === 'admin' && password === 'admin' || username === 'user' && password === 'user') {
        return [200, { id_token: `id_token:${username}` }];
      }
      return [401, {}];
    });
  const urlRegex = new RegExp(`${baseApiUrl}randomImage/\\d+`);
  mock
    ?.onGet(urlRegex)
    .reply((config) => {
      console.log('randomImage.config:::', config);
      return [200, 'data:image/jpg;base64, /9j/4 AAQSkZJRgABAgAAAQABAAD/2 wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2 wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAjAGkDASIAAhEBAxEB/8 QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8 QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8 QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8 QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzU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EEVtEIoUCRgkhR0GTnj86cI1ErSAfOwCk+wzj+ZoooAEijj3eWipuYs20YyT3PvSJFHFu8tFTcxZtoxknqT70UUAOZVcYZQwyDgjPIOR+tLRRQB //9k='];
    });
  mock
    ?.onGet('/api/account')
    .reply((config) => {
      console.log('account.config:::', config);
      fetchData();
      const user = allData.find(user => user.login === 'admin') || {};
      console.log('user.mock:::', user);
      return [200, user];
    });
}
