import{a as O}from"./chunk-2WDKXDFM.js";import{a as I}from"./chunk-RFDT525K.js";import{c as _,i as P,n as k}from"./chunk-KOB6T6NK.js";import{b as S,m as b}from"./chunk-5T3OVTF5.js";import{j as g,k as T,l as L,p as R,v as q}from"./chunk-PTIJDS6I.js";import{xa as x}from"./chunk-2HZMOK4U.js";import{a as v}from"./chunk-J3KRY2SD.js";import{b as E}from"./chunk-Q6LCSRIW.js";import{d as u}from"./chunk-ELIH6GCC.js";import{Fb as d,Hb as m,Mb as o,Pb as c}from"./chunk-EYDNSYYE.js";import{r as G}from"./chunk-T6CC5WUU.js";import{e as V,g as r}from"./chunk-VKWPEFWQ.js";r();r();r();var f=class extends I{constructor(t){super(t),this.context="REPL",this.themeId=g()}async create(t,n){let s=await super.create(t,n);return await this.uploadThemeAssets(s),s}async findOrCreate(){let t=await this.fetch();if(!t){let n=`Liquid Console (${v})`;t=await this.create(S,n)}return t}setTheme(t){T(t)}removeTheme(){L()}async uploadThemeAssets(t){let n=[{key:"config/settings_data.json",value:"{}"},{key:"config/settings_schema.json",value:"[]"},{key:"snippets/eval.liquid",value:""},{key:"layout/password.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"},{key:"layout/theme.liquid",value:"{{ content_for_header }}{{ content_for_layout }}"},{key:"sections/announcement-bar.liquid",value:""},{key:"templates/index.json",value:JSON.stringify({sections:{announcement:{type:"announcement-bar",settings:{}}},order:["announcement"]})}];await b(t.id,n,this.adminSession)}};r();r();async function A(e){return await H(e)??await $(e)??await Y(e)??await z(e)??void 0}async function H(e){c(`Evaluating snippet - ${e.snippet}`);let t=`{ "type": "display", "value": {{ ${e.snippet} | json }} }`,n=await h({...e,snippet:t}),s=await n.text();return C(n.status,s)?Q(s):void 0}async function $(e){c(`Evaluating context - ${e.snippet}`);let t=`{ "type": "context", "value": "{% ${e.snippet.replace(/"/g,'\\"')} %}" }`,n=await h({...e,snippet:t}),s=await n.text();C(n.status,s)&&e.replSession.push(JSON.parse(t))}async function Y(e){if(c(`Evaluating assignment context - ${e.snippet}`),oe(e.snippet))return e.snippet=`assign ${e.snippet}`,o(m`${d.gray(`> ${e.snippet}`)}`),$(e)}async function z(e){c(`Evaluating syntax error - ${e.snippet}`);let t="";if(X(e.snippet)||(t=await(await h({...e,snippet:`{{ ${e.snippet} }}`})).text()),y(t)||(t=await(await h({...e,snippet:`{% ${e.snippet} %}`})).text()),y(t)){let n=t.replace(/ \(snippets\/eval line \d+\)/,"");B(e.snippet,n)}}function B(e,t){if(t.includes("Unknown tag")){o(m`${d.errorText(`Unknown object, property, tag, or filter: '${e}'`)}`);return}let n=j(t);n&&o(m`${d.errorText(n)}`)}async function h(e){let t=K(e);e.url.startsWith("/")||(e.url=`/${e.url}`);let n=await P(e.themeSession,{method:"GET",path:e.url,query:[],themeId:e.themeId,sectionId:"announcement-bar",headers:{},replaceTemplates:{"sections/announcement-bar.liquid":"{% render 'eval' %}","snippets/eval.liquid":`
${t}
`}});return Z(n)&&ne(),ee(n)&&se(),te(n)&&re(),n}function K(e){return`[${[...e.replSession.map(n=>JSON.stringify(n)),e.snippet].join(",").replace(/\\"/g,'"')}]`}function Q(e){let t=j(e);if(t)return JSON.parse(t)?.find(s=>s.type==="display")?.value}function j(e){let t=e.split(`
`).slice(1,-1);if(t.length!==0)return t.join("")}function y(e){return e.includes("Liquid syntax error")}function X(e){return/^\s*assign\s*((?:\(?[\w\-.[\]]\)?)+)\s*=\s*(.*)\s*/m.test(e)}function Z(e){return e.status===401||e.status===403}function ee(e){return e.status===430||e.status===429}function te(e){return e.headers.get("server-timing")?.includes('pageType;desc="404"')??!1}function ne(){throw new u("Session expired. Please initiate a new one.")}function se(){throw new u("Evaluations limit reached. Try again later.")}function re(){throw new u("Page not found. Please provide a valid --url value!")}function oe(e){return/^\s*((?:\(?[\w\-.[\]]\)?)+)\s*=\s*(.*)\s*/m.test(e)}function C(e,t){return e===200&&!y(t)}r();function F(e){if(N(e)){o("Object can't be printed, but you can access its fields. Read more at https://shopify.dev/docs/api/liquid.");return}if(e==null){M("null");return}let t=JSON.stringify(e,null,2);M(t)}function N(e){if(Array.isArray(e))return e.length>0?N(e[0]):!1;if(e&&typeof e=="object"){let n=e.error;return typeof n=="string"&&n.includes("json not allowed for this object")}return!1}function M(e){return o(m`${d.cyan(e)}`)}import{createInterface as ie}from"readline";var ae=`Liquid Console doesn't support Liquid delimiters such as '{{ ... }}' or '{% ... %}'.
Please use 'collections.first' instead of '{{ collections.first }}'.`;async function D(e,t,n){process.stdin.isTTY&&process.stdin.ref();let s=ie({input:process.stdin,output:process.stdout}),a=[];s.on("line",p=>{ue(p,e,t,n,s,a)}),s.prompt()}async function ue(e,t,n,s,a,p){try{if(pe(e))return o(ae),a.prompt();let l=await A({snippet:e,themeSession:t,themeId:n,url:s,replSession:p});F(l),a.prompt()}catch(l){throw a.close(),l instanceof Error?(c(l.stack??"Error backtrace not found"),new u(l.message)):new u("An unknown error occurred. Please try again.")}}function pe(e){return/^\s*(\{\{|{%)/.test(e)}async function W(e,t){let n=await le(e),s=await _(e)?await O(t,e.storeFqdn):void 0;return{themeId:n,storePassword:s}}async function le(e){return(await new f(e).findOrCreate()).id.toString()}async function J(e,t,n,s,a){if(o(`Welcome to Shopify Liquid console
(press Ctrl + C to exit)`),s?.startsWith("shpat_"))throw new u("Unable to use Admin API tokens with the console command",`To use this command with the --password flag you must:

1. Install the Theme Access app on your shop
2. Generate a new password

Alternatively, you can authenticate normally by not passing the --password flag.

Learn more: https://shopify.dev/docs/storefronts/themes/tools/theme-access`);let p=await k(t,e,s,a);return D(p,t,n)}var w=V(G(),1);var U,i=class extends q{async command(t,n){let{url:s,password:a}=t,{themeId:p,storePassword:l}=await W(n,t["store-password"]);x("theme-command:console:single-env:authenticated"),await J(n,p,s,a,l)}};U=i;i.summary="Shopify Liquid REPL (read-eval-print loop) tool";i.usage=["theme console","theme console --url /products/classic-leather-jacket"];i.descriptionWithMarkdown=`Starts the Shopify Liquid REPL (read-eval-print loop) tool. This tool provides an interactive terminal interface for evaluating Liquid code and exploring Liquid objects, filters, and tags using real store data.

  You can also provide context to the console using a URL, as some Liquid objects are context-specific`;i.description=U.descriptionWithoutMarkdown();i.flags={...E,...R,url:w.Flags.string({description:"The url to be used as context",env:"SHOPIFY_FLAG_URL",default:"/"}),"store-password":w.Flags.string({description:"The password for storefronts with password protection.",env:"SHOPIFY_FLAG_STORE_PASSWORD"})};i.multiEnvironmentsFlags=null;var Ye=i;export{Ye as a};
