import sql from 'mssql';

let activeShopId = 0;
let activeShopSubshopId = 0;

export function setActiveShop(id) {
  activeShopId = id;
}

export function getActiveShopId() {
  return activeShopId;
}

export function setActiveShopSubshop(id) {
  activeShopSubshopId = id;
}

export function getActiveShopSubshopId() {
  return activeShopSubshopId;
}

export async function fetchActiveShop(pool) {
  const result = await pool.request().query(`
    SELECT TOP 1 kShop, kShopSubshop FROM dbo.tShopSubshop WHERE nGesperrt = 0 ORDER BY kShop
  `);
  const id = result.recordset[0]?.kShop ?? 0;
  setActiveShop(id);
  setActiveShopSubshop(result.recordset[0]?.kShopSubshop ?? 0);
  return id;
}
