import { AxiosRequestConfig } from 'axios'
import * as yup from 'yup'
import axios from '../../config/axios'
export const defaultFilterObj = {
  filters: {
    search: null,
    limit: 10,
    offset: 0,
  },
  filtersApplied: false,
}

export const userProfileSchema = yup.object().shape({
  userId: yup.array().min(1).required('User is required'),
  profileIds: yup.array().min(1).required('Profile is required'),
})

export const editUserProfileSchema = yup.object().shape({
  profileIds: yup.array().min(1).required('Profile is required'),
})
export const fetchUsers = (params) => {
  return axios
    .get(`/square/profile-permissions/new-users`, {
      params: params,
    })
    .then((res) => res.data)
}

export const fetchApplicationUsers = (params) => {
  return axios
    .get(`/square/profile-permissions/application-users`, {
      params: params,
    })
    .then((res) => res.data)
}

export const fetchProfiles = (application) => {
  return axios
    .get(`/square/profiles`, {
      params: application,
    })
    .then((res) => res.data)
}

interface ApplicationUserProfile {
  userId?: number
  application: string
  userIds: number[]
  profileIds: number[]
}
export const createApplicationUserProfile = (
  postBody: ApplicationUserProfile,
) => {
  return axios
    .post(`/square/profile-permissions/add-user`, postBody)
    .then((res) => res.data)
}

export const removeUserApplicationProfile = (postBody) => {
  return axios
    .post(`/square/profile-permissions/remove-user`, postBody)
    .then((res) => res.data)
}

export const updateCreateUserApplicationProfile = (
  postBody: ApplicationUserProfile,
) => {
  const config: AxiosRequestConfig = {
    method: postBody?.userId ? 'PUT' : 'POST',
    url: postBody?.userId
      ? `/square/profile-permissions/edit-user-permissions`
      : '/square/profile-permissions/add-user',
    data: postBody,
  }
  return axios(config).then((res) => res.data)
}
