import axios from 'axios';
import { Cookies } from 'react-cookie';
import Router from 'next/router';
import moment from 'moment';

import * as actionTypes from '../constants/actionTypes';

if(process.browser)
{
	FormData.prototype.appendObject = function(obj, namespace) 
	{
		let keyName;
		for (var key in obj) 
		{
			if (obj.hasOwnProperty(key)) 
			{
				keyName = [namespace, '[', key, ']'].join('');
				this.append(keyName, obj[key]);
			}
		}
	};	
}

export const getCompanyInfo = ({ dispatch }) => 
{
	console.log("getCompanyInfo");
	return new Promise((resolve, reject) => 
	{
		axios.post(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/company`, {}, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("getCompanyInfo", "response", response.data);

			dispatch({ type: actionTypes.COMPANY, data: response.data });
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}
