

function hasPermission(userPermissions, permission) {
  return userPermissions.includes(permission);
}

function hasAnyPermission(userPermissions, requiredPermissions) {
  return requiredPermissions.some(permission => userPermissions.includes(permission));
}

// Function to load company name from API and store in localStorage
async function loadCompanyName() {
  const companyId = sessionStorage.getItem('companyId');
  const branchId = sessionStorage.getItem('branchId');
  
  if (!companyId || !branchId) {
    // If no company/branch set, use default from localStorage or 'Zad'
    const storedName = localStorage.getItem('companyName');
    return storedName || 'Zad';
  }
  
  try {
    const response = await fetch(`/api/settings?companyId=${companyId}&branchId=${branchId}`);
    if (response.ok) {
      const data = await response.json();
      if (data.companyName) {
        localStorage.setItem('companyName', data.companyName);
        return data.companyName;
      }
    }
  } catch (error) {
    console.error('Error loading company name:', error);
  }
  
  // Fallback to stored value or default
  return localStorage.getItem('companyName') || 'Zad';
}

// Function to get company name from localStorage with fallback
function getCompanyName() {
  return localStorage.getItem('companyName') || 'Zad';
}

// Function to load company logo from API and store in localStorage
async function loadCompanyLogo() {
  const companyId = sessionStorage.getItem('companyId');
  const branchId = sessionStorage.getItem('branchId');
  
  if (!companyId || !branchId) {
    // If no company/branch set, use default from localStorage
    return localStorage.getItem('companyLogo') || '/uploads/cyber.jpeg';
  }
  
  try {
    const response = await fetch(`/api/settings?companyId=${companyId}&branchId=${branchId}`);
    if (response.ok) {
      const data = await response.json();
      if (data.companyLogo) {
        localStorage.setItem('companyLogo', data.companyLogo);
        return data.companyLogo;
      }
    }
  } catch (error) {
    console.error('Error loading company logo:', error);
  }
  
  // Fallback to stored value or default
  return localStorage.getItem('companyLogo') || '/uploads/cyber.jpeg';
}

// Function to get company logo from localStorage with fallback
function getCompanyLogo() {
  return localStorage.getItem('companyLogo') || '/uploads/cyber.jpeg';
}

// Encapsulate and expose as a module-like object
const permissionUtils= {

  hasPermission,
  hasAnyPermission,
};

// Export the PermissionsUtils object
export default permissionUtils;
