'use client';
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var jsxRuntime=require("react/jsx-runtime"),core=require("@gem-sdk/core"),ChildrenDroppable=require("../../grid/components/ChildrenDroppable.js"),Loading=require("../../common/components/Loading.js"),React=require("react"),ArticleNotFound=require("./ArticleNotFound.js");const ArticleList=e=>{let t=core.useEditorMode(),{styles:i,setting:r,className:l,children:o,builderProps:a,advanced:s,builderAttrs:n,style:d}=e??{},{articleSetting:c,numberOfArticle:u}=r??{},{articleIds:m,articlePickType:g}=c??{},{border:p,rounded:h,hasBoxShadow:b,boxShadow:y}=s??{},[x]=core.splitStyle(["pos","top","left","bottom","right","z","d"],d),j=core.composeAdvanceStyle({border:p,rounded:h,hasBoxShadow:b,boxShadow:y,"spacing-setting":s?.["spacing-setting"]}),v={first:m?.length||50,where:{baseIDIn:[...c?.articleIds??[]]},orderBy:{direction:"DESC",field:"PLATFORM_UPDATED_AT"}},f={first:1,where:{baseID:c?.blogId},orderBy:{direction:"DESC",field:"PLATFORM_UPDATED_AT"},articlesOrderBy:{direction:"DESC",field:"PLATFORM_UPDATED_AT"}},[R,S]=React.useState(!0),A=core.useBlogsQuery("latest"===g?f:null,{revalidateOnMount:!0}),D=core.useArticlesQuery("manually"===g?v:null,{revalidateOnMount:!0}),w=A.data?.blogs?.edges?.[0]?.node,T="manually"===g?D.isLoading:A.isLoading,k="manually"===g?D.isValidating:A.isValidating,B="manually"===g?D.error:A.error,P=A?.data?.blogs?.edges?.[0]?.node,L="latest"===g?core.flattenConnection(P?.articles):core.flattenConnection(D?.data?.articles),C=(e,t)=>({...e??{},blog:{title:t?.title??"",handle:t?.handle??""}}),O="latest"===g?L?.filter((e,t)=>t<+(u||4)).map(e=>C(e,P)):"manually"===g&&c?.articleIds?.length?m?.map(e=>{let t=L?.find(t=>t?.baseID===e);return C(t,t?.blogs?.edges?.[0]?.node)}).filter(Boolean):L.filter((e,t)=>t<(c?.numOfSelectedArticles||4)).map(e=>C(e,e?.blogs?.edges?.[0]?.node));return O?.length||T||"edit"!=t?jsxRuntime.jsxs("div",{...n,className:a?.builderData?.uid,style:x,children:[k||T?jsxRuntime.jsx(Loading.default,{}):B?jsxRuntime.jsxs("div",{children:["Error: ",B?.message]}):O?.length?jsxRuntime.jsx(core.ArticleListProvider,{styles:{horizontalGutter:i?.horizontalGutter,verticalGutter:i?.verticalGutter,spacing:i?.spacing,width:i?.width,height:i?.height},settings:{slidesToShow:r?.slideToShow??{desktop:4,tablet:3,mobile:1}},articles:O,children:jsxRuntime.jsx("div",{"data-id":a?.uid,className:"gp-relative",children:jsxRuntime.jsx("div",{style:{...j,...core.makeStyleWithDefault("w",core.makeWidth(i?.width),{desktop:"--g-ct-w",tablet:"--g-ct-w",mobile:"--g-ct-w"}),...core.makeStyleResponsive("rg",i?.horizontalGutter),...core.makeStyleResponsive("cg",i?.verticalGutter),...core.getStyleBackgroundByDevice(r?.background),...core.getGradientBgrStyleByDevice(r?.background),...core.makeStyleResponsive("gtc",{desktop:`repeat(${core.getResponsiveValueByScreen(r?.slideToShow,"desktop")}, minmax(0, 1fr))`,tablet:r?.slideToShow?.tablet&&`repeat(${core.getResponsiveValueByScreen(r?.slideToShow,"tablet")}, minmax(0, 1fr))`,mobile:r?.slideToShow?.mobile&&`repeat(${core.getResponsiveValueByScreen(r?.slideToShow,"mobile")}, minmax(0, 1fr))`})},className:core.cls(l,"gp-m-auto gp-grid gp-w-full"),children:O.map(e=>jsxRuntime.jsx(core.ArticleProvider,{article:e,readOnly:!0,children:jsxRuntime.jsx("div",{"data-component-type":"component","data-component-tag":"Article","data-component-label":"Article","data-component-no-setting":!0,"data-article-id":e?.baseID?.replace("gid://shopify/OnlineStoreArticle/",""),children:core.isEmptyChildren(o)?jsxRuntime.jsx(ChildrenDroppable.default,{}):o})},e?.id))})})}):jsxRuntime.jsx("div",{children:"Articles not found"}),core.filterToolbarPreview(o,!0)]}):R?jsxRuntime.jsxs("div",{...n,className:a?.builderData?.uid,children:[jsxRuntime.jsx(ArticleNotFound.default,{blogHandle:w?.handle,onClose:()=>S(!1),articlePickType:g??"manually",children:o}),core.filterToolbarPreview(o,!0)]}):null};exports.default=ArticleList;
