import { C as error, O as fail, N as redirect } from '../../../../../chunks/utils.js-mIi_c4Oz.js';
import { a as hasRole } from '../../../../../chunks/auth.js-D502gejJ.js';
import { d as db } from '../../../../../chunks/db.js-le-jjQPv.js';
import { e as readDeviceForm, u as updateDevice, l as listCategories } from '../../../../../chunks/queries.js-BUUEc74j.js';

const load = async ({ params, locals }) => {
  if (!hasRole(locals.user, "gestor")) throw error(403, "Sin permisos.");
  const device = db.prepare("SELECT * FROM devices WHERE id = ?").get(Number(params.id));
  if (!device) throw error(404, "Equipo no encontrado.");
  return { device, categories: listCategories() };
};
const actions = {
  default: async ({ request, params, locals }) => {
    if (!hasRole(locals.user, "gestor")) throw error(403, "Sin permisos.");
    const id = Number(params.id);
    const form = await request.formData();
    const input = readDeviceForm(form);
    const res = updateDevice(id, input);
    if (res.error) {
      return fail(400, { error: res.error, values: Object.fromEntries(form) });
    }
    throw redirect(303, `/devices/${id}`);
  }
};

var _page_server_ts = /*#__PURE__*/Object.freeze({
  __proto__: null,
  actions: actions,
  load: load
});

export { _page_server_ts as _ };
//# sourceMappingURL=_page.server.ts.js-Bu5AVt5Q.js.map
