"use strict";var t,e=require("axios"),s=require("user-agents"),a=require("axios-cookiejar-support"),i=require("tough-cookie"),r=require("lodash"),n=require("os");exports.Status=void 0,(t=exports.Status||(exports.Status={})).ERROR="error",t.STREAMING="streaming",t.COMPLETE="complete";const o="chirp-v3-5",c="5.26.1",p="https://studio-api.suno.ai/api",u="https://clerk.suno.com/v1/client";var l=Object.freeze({__proto__:null,API_BASE_URL:p,CLERK_API_BASE_URL:u,CLERK_JS_VERSION:c,DEFAULT_MODEL:o});const h=t=>Array.isArray(t)?t.map((t=>h(t))):null!==t&&"object"==typeof t?Object.keys(t).reduce(((e,s)=>(e[r.camelCase(s)]=h(t[s]),e)),{}):t,d=t=>Array.isArray(t)?t.map((t=>d(t))):null!==t&&"object"==typeof t?Object.keys(t).reduce(((e,s)=>(e[r.snakeCase(s)]=d(t[s]),e)),{}):t,w=(t,e)=>{let s=1e3*t;if(void 0!==e&&e!==t){const a=Math.min(t,e),i=Math.max(t,e);s=1e3*Math.floor(Math.random()*(i-a+1)+a)}return new Promise((t=>setTimeout(t,s)))},C=t=>t.split("\n").filter((t=>""!==t.trim())).join(n.EOL),g=1e4;exports.Api=class Api{clerkClient;apiClient;accessToken;constructor(t){const r=new s(/Chrome/).random().toString();this.clerkClient=a.wrapper(e.create({baseURL:u,jar:new i.CookieJar,withCredentials:!0,timeout:g,headers:{"User-Agent":r,Cookie:t}})),this.apiClient=e.create({baseURL:p,withCredentials:!0,timeout:g,headers:{"User-Agent":r}}),this.apiClient.interceptors.request.use((t=>(this.accessToken&&(t.headers.Authorization=`Bearer ${this.accessToken}`),t))),this.apiClient.interceptors.response.use((t=>t),(async t=>{if(t.response&&401===t.response.status)try{return await this.authenticate(),await this.apiClient.request(t.response.config)}catch(t){return Promise.reject(t)}return Promise.reject(t)}));const n=t=>(t.data&&(t.data=d(t.data)),t.params&&(t.params=d(t.params)),t);this.apiClient.interceptors.request.use(n),this.clerkClient.interceptors.request.use(n);const o=t=>(t.data&&(t.data=h(t.data)),t);this.apiClient.interceptors.response.use(o),this.clerkClient.interceptors.response.use(o),this.apiClient.interceptors.response.use((t=>200===t.status?t:Promise.reject(new Error(`Unexpected status code[${t.status}]: ${t.statusText}`))))}async authenticate(t=!1){const e=(await this.clerkClient.get(`?_clerk_js_version=${c}`)).data;if(void 0===e?.response?.lastActiveSessionId)throw new Error("Failed to get session id, you may need to update the SUNO_COOKIE");const{jwt:s}=(await this.clerkClient.post(`/sessions/${e.response.lastActiveSessionId}/tokens?_clerk_js_version=${c}`)).data;this.accessToken=s,t&&await w(1,2)}async waitGeneration(t,e,s){if(!e.wait)return s;const a=Date.now();let i;const[r,n]=e.waitSleepRange||[10,20];await w(r,n);const o=e.waitTimeout||6e5,c=e.waitStatuses||[exports.Status.STREAMING,exports.Status.COMPLETE];for(;Date.now()-ac.includes(t))),o=s.every((({status:t})=>t===exports.Status.ERROR));if(a||o)return e;i=e,await w(r,n)}return i}async generateClips(t,e={wait:!0,waitStatuses:[exports.Status.COMPLETE],waitSleepRange:[10,20]}){const{clips:s}=(await this.apiClient.post("/generate/v2/",{mv:o,makeInstrumental:!1,...t,...t.tags&&t.title?{}:{gptDescriptionPrompt:prompt}})).data,a=s.map((({id:t})=>t)).join(",");return await this.waitGeneration(this.getFeed.bind(this,{ids:a}),e,s)}async extendClip(t,e={wait:!0,waitStatuses:[exports.Status.STREAMING,exports.Status.COMPLETE],waitSleepRange:[10,20]}){const s=(await this.apiClient.post("/generate/v2/",t)).data;return await this.waitGeneration(this.getClip.bind(this,s.id),e,s)}async concatenate(t){return(await this.apiClient.post("/generate/concat/v2/",{clipId:t})).data}async generateLyrics(t,e={wait:!0,waitStatuses:[exports.Status.COMPLETE],waitSleepRange:[2,5]}){const{id:s}=(await this.apiClient.post("/generate/lyrics/",t)).data;return await this.waitGeneration(this.getLyrics.bind(this,s),e,{id:s,text:"Can not generate lyrics",title:"",status:exports.Status.ERROR})}async getLyrics(t){const e=(await this.apiClient.get(`/generate/lyrics/${t}`)).data;return{id:t,...e,text:C(e.text||"")}}async getFeed(t){return(await this.apiClient.get("/feed/v2",{params:t})).data.clips}async getClip(t){return(await this.apiClient.get(`/clip/${t}`)).data}async getBillingInfo(){return(await this.apiClient.get("/billing/info/")).data}},exports.DEFAULT_AXIOS_TIMEOUT=g,exports.constants=l;