import{a as X,b as H,c as K}from"./chunk-ALQKJAHP.js";import{a as M,c as P,e as h,f as T,h as q,i as V,m as U,p as _,q as F,r as j,s as z,u as Q}from"./chunk-5UXPTCSD.js";import{a as W}from"./chunk-6ZRHK4ON.js";import{a as G,b as J}from"./chunk-7XQDZF73.js";import{b as L}from"./chunk-Q6LCSRIW.js";import{E as R,a as D,u as v,v as d,w as E,x as A}from"./chunk-E24W34C4.js";import{d as f,g as x}from"./chunk-ELIH6GCC.js";import{Fb as r,Hb as s,Lb as N,Ta as $,ma as S,ua as I}from"./chunk-EYDNSYYE.js";import{g as m}from"./chunk-VKWPEFWQ.js";m();m();async function oe(t,e){if(t)return t.join(`
`);if(e){if(!await $(e))throw new f(s`Variable file not found at ${r.path(e)}. Please check the path and try again.`);return S(e,{encoding:"utf8"})}else return}async function Z(t){let{organization:e,remoteApp:i,store:n,query:a,variables:p,variableFile:k,outputFile:B,watch:ee=!1,version:te}=t,{adminSession:c,version:w}=await R({title:s`Authenticating`,task:async()=>{let o=await X(i,n.shopDomain),u=await T({adminSession:o,userSpecifiedVersion:te,minimumDefaultVersion:M});return{adminSession:o,version:u}},renderOptions:{stdout:process.stderr}}),C=await oe(p,k);ae(a,C),K(a,n),v({headline:"Starting bulk operation.",body:[{list:{items:H({organization:e,remoteApp:i,storeFqdn:n.shopDomain,version:w})}}]});let y=h(a)?await V({adminSession:c,query:a,variablesJsonl:C,version:w}):await q({adminSession:c,query:a,version:w});if(y?.userErrors?.length){A({headline:"Error creating bulk operation.",body:{list:{items:y.userErrors.map(o=>o.field?`${o.field.join(".")}: ${o.message}`:o.message)}}});return}let g=y?.bulkOperation;if(g)if(ee){let o=new D,u=await F(c,g.id,o.signal,()=>o.abort());o.signal.aborted?v({headline:`Bulk operation ${u.id} is still running in the background.`,body:b(u.id)}):await Y(u,B)}else{let o=await _(c,g.id);["FAILED","CANCELED","EXPIRED"].includes(o.status)?await Y(o,B):d({headline:"Bulk operation is running.",body:b(o.id),customSections:[{body:[{list:{items:[s`ID: ${r.cyan(o.id)}`.value]}}]}]})}else throw E({headline:"Bulk operation not created successfully.",body:"This is an unexpected error. Please try again later."}),new x("Bulk operation response returned null with no error message.")}async function Y(t,e){let i=U(t).value,a=[{body:[{list:{items:[s`ID: ${r.cyan(t.id)}`.value,s`Status: ${r.yellow(t.status)}`.value,s`Created at: ${r.gray(String(t.createdAt))}`.value,...t.completedAt?[s`Completed at: ${r.gray(String(t.completedAt))}`.value]:[]]}}]}];switch(t.status){case"CREATED":d({headline:"Bulk operation started.",body:b(t.id),customSections:a});break;case"RUNNING":d({headline:"Bulk operation is running.",body:b(t.id),customSections:a});break;case"COMPLETED":if(t.url){let p=await j(t.url),k=z(p);e?await I(e,p):N(p),k?E({headline:"Bulk operation completed with errors.",body:e?`Results written to ${e}. Check file for error details.`:"Check results for error details.",customSections:a}):d({headline:i,body:e?[`Results written to ${e}`]:void 0,customSections:a})}else d({headline:i,customSections:a});break;case"CANCELED":case"CANCELING":case"EXPIRED":case"FAILED":A({headline:i,customSections:a});break}}function ae(t,e){if(h(t)&&!e)throw new f(s`Bulk mutations require variables. Provide a JSONL file with ${r.yellow("--variable-file")} or individual JSON objects with ${r.yellow("--variables")}.`);if(!h(t)&&e)throw new f(s`The ${r.yellow("--variables")} and ${r.yellow("--variable-file")} flags can only be used with mutations, not queries.`)}function b(t){return[`Monitor its progress with:
`,{command:`shopify app bulk status --id=${P(t)}`}]}var O,l=class extends W{async run(){let{flags:e}=await this.parse(O),{query:i,appContextResult:n,store:a}=await Q(e);return await Z({organization:n.organization,remoteApp:n.remoteApp,store:a,query:i,variables:e.variables,variableFile:e["variable-file"],watch:e.watch??!1,outputFile:e["output-file"],...e.version&&{version:e.version}}),{app:n.app}}};O=l;l.summary="Execute bulk operations.";l.descriptionWithMarkdown=`Executes an Admin API GraphQL query or mutation on the specified store, as a bulk operation. Mutations are only allowed on dev stores.

  Bulk operations allow you to process large amounts of data asynchronously. Learn more about [bulk query operations](https://shopify.dev/docs/api/usage/bulk-operations/queries) and [bulk mutation operations](https://shopify.dev/docs/api/usage/bulk-operations/imports).

  Use [\`bulk status\`](https://shopify.dev/docs/api/shopify-cli/app/app-bulk-status) to check the status of your bulk operations.`;l.description=O.descriptionWithoutMarkdown();l.flags={...L,...G,...J};var we=l;export{we as a};
