"use strict";var h=Object.defineProperty;var R=(p,e)=>h(p,"name",{value:e,configurable:true});const u=use("App/Models/Recipe");const f=use("Drive");const{validateAll:w}=use("Validator");const I=use("Env");const g=require("../../../../preload-safe-debug")("Ferdium:internalServer:RecipeController");const{LIVE_FERDIUM_API:E}=require("../../../../config");const{convertToJSON:r}=require("../../../../jsUtils");const{API_VERSION:N}=require("../../../../environment-remote");const d=`${E}/${N}/recipes`;class v{async list({response:e}){const s=await fetch(d);const a=r(await s.text());const t=await u.all();const i=t.rows;const c=i.map(n=>({id:n.recipeId,name:n.name,...r(n.data)}));const o=[...a,...c];return e.send(o)}async search({request:e,response:s}){const a=await w(e.all(),{needle:"required"});if(a.fails()){return s.status(401).send({message:"Please provide a needle",messages:a.messages(),status:401})}const t=e.input("needle");let i;if(t==="ferdium:custom"){const c=await u.all();const o=c.toJSON();i=o.map(n=>({id:n.recipeId,name:n.name,...r(n.data)}))}else{let c=[];if(I.get("CONNECT_WITH_FRANZ")==="true"){const l=await fetch(`${d}/search?needle=${encodeURIComponent(t)}`);c=r(await l.text())}g("remoteResults:",c);const o=await u.query().where("name","LIKE",`%${t}%`).fetch();const n=o.toJSON();const m=n.map(l=>({id:l.recipeId,name:l.name,...r(l.data)}));g("localResults:",m);i=[...m,...c||[]]}return s.send(i)}update({response:e}){return e.send([])}async popularRecipes({response:e}){const s=await fetch(`${d}/popular`);const a=r(await s.text());return e.send(a)}async download({response:e,params:s}){const a=await w(s,{recipe:"required|accepted"});if(a.fails()){return e.status(401).send({message:"Please provide a recipe ID",messages:a.messages(),status:401})}const t=s.recipe;if(/\.+/.test(t)||/\/+/.test(t)){return e.send("Invalid recipe name")}if(await f.exists(`${t}.tar.gz`)){return e.send(await f.get(`${t}.tar.gz`))}if(I.get("CONNECT_WITH_FRANZ")==="true"){return e.redirect(`${d}/download/${t}`)}return e.status(400).send({message:"Recipe not found",code:"recipe-not-found"})}}R(v,"RecipeController");module.exports=v;
