function g(t){if(t!=null){if(typeof t=="string"){const r=t.trim();return r.length===0?void 0:r}if(Array.isArray(t)){const r=t.map(n=>g(n)).filter(n=>n!==void 0);return r.length===0?void 0:r}if(typeof t=="object"){const r=t,n={};for(const i of Object.keys(r)){const a=g(r[i]);a!==void 0&&(n[i]=a)}return Object.keys(n).length===0?void 0:n}return t}}function v(t){return{"@id":t+"#organization"}}function b(t,r,n,i,a,e){const d=t.name&&t.name.trim()||r,p=void 0??(e?v(e):void 0);if(!d||!p)return null;const s={"@context":"https://schema.org","@type":"Service",name:d,description:t.description&&t.description.trim()||n,serviceType:t.serviceType,priceRange:t.priceRange,areaServed:t.areaServed,url:i,provider:p};return g(s)??null}function A(t){const r=(t||[]).filter(a=>a&&typeof a.question=="string"&&a.question.trim().length>0&&typeof a.answer=="string"&&a.answer.trim().length>0);if(r.length===0)return null;const n={"@context":"https://schema.org","@type":"FAQPage",mainEntity:r.map(a=>({"@type":"Question",name:a.question.trim(),acceptedAnswer:{"@type":"Answer",text:a.answer.trim()}}))};return g(n)??null}function E(t,r,n,i,a){const e=t.headline&&t.headline.trim()||n,d=void 0??(a?v(a):void 0);if(!e||!d)return null;const p=t.author&&t.author.trim().length>0?{"@type":"Person",name:t.author.trim()}:d,s={"@context":"https://schema.org","@type":"Article",headline:e,datePublished:t.datePublished,dateModified:t.dateModified||t.datePublished,image:t.image,author:p,publisher:d,mainEntityOfPage:{"@type":"WebPage","@id":r},url:r};return g(s)??null}function S(t,r){const n=(t||[]).filter(e=>e&&typeof e.name=="string"&&e.name.trim().length>0);if(n.length===0)return null;const i={"@context":"https://schema.org","@type":"BreadcrumbList",itemListElement:n.map((e,d)=>{const p={"@type":"ListItem",position:d+1,name:e.name.trim()};if(e.url&&e.url.trim().length>0){const s=e.url.trim();p.item=/^https?:\/\//i.test(s)?s:r+(s.startsWith("/")?s:"/"+s)}return p})};return g(i)??null}function m(t,r,n){let i=document.querySelector(`meta[${t}="${r}"]`);if(!n){i&&i.setAttribute("content","");return}i||(i=document.createElement("meta"),i.setAttribute(t,r),document.head.appendChild(i)),i.setAttribute("content",n)}function q(t,r,n){document.querySelectorAll(`meta[${t}="${r}"]`).forEach(i=>i.remove()),n.map(i=>typeof i=="string"?i.trim():"").filter(i=>i.length>0).forEach(i=>{const a=document.createElement("meta");a.setAttribute(t,r),a.setAttribute("content",i),document.head.appendChild(a)})}function w(){if(typeof document>"u")return"";const t=document.getElementById("global-organization-schema");if(!t||!t.textContent)return"";try{const r=JSON.parse(t.textContent),n=Array.isArray(r)?r[0]:r;return n&&typeof n=="object"&&typeof n.name=="string"?n.name:""}catch{return""}}function j(t){if(!t)return;let r=document.querySelector('link[rel="canonical"]');r||(r=document.createElement("link"),r.setAttribute("rel","canonical"),document.head.appendChild(r)),r.setAttribute("href",t)}function P(t,r){const n=t.trim();if(n.length===0)return"";if(/^https?:\/\//i.test(n))return n;const i=n.startsWith("/")?n:`/${n}`;return r+i}const k=["page-schema","breadcrumb"];function y(t,r){const n=`script[type="application/ld+json"][data-seo-key="${t}"]`,i=document.querySelector(n);if(r===null){i&&i.remove();return}const a=JSON.stringify(r,null,2);if(i){i.textContent=a;return}const e=document.createElement("script");e.type="application/ld+json",e.setAttribute("data-seo-key",t),e.textContent=a,document.head.appendChild(e)}function x(){k.forEach(t=>y(t,null)),document.querySelectorAll('script[type="application/ld+json"][data-page]').forEach(t=>t.remove()),document.querySelectorAll('script[type="application/ld+json"][data-breadcrumb]').forEach(t=>t.remove()),document.querySelectorAll('script[type="application/ld+json"][data-seo-key="organization"]').forEach(t=>t.remove())}function T(t,r,n,i){var d,p,s,f;const a=((d=r.seo)==null?void 0:d.title)||r.title||"",e=((p=r.seo)==null?void 0:p.description)||"";if((t==null?void 0:t.type)==="none")return null;if(!t||!t.type){if(r.template==="service"){const o={serviceType:((s=r.seo)==null?void 0:s.articleSection)||""},c=b(o,a,e,n,null,i);return c?(c.provider={"@id":i+"#organization"},(f=r.seo)!=null&&f.ogImage&&(c.image=r.seo.ogImage),c):null}return null}if(t.type==="Service"){const o={name:t.name,priceRange:t.priceRange,description:t.description,serviceType:t.serviceType,areaServed:Array.isArray(t.areaServed)?t.areaServed:void 0},c=b(o,a,e,n,null,i);return c?(c.provider={"@id":i+"#organization"},c):null}if(t.type==="FAQ"){const c=(t.items||[]).filter(u=>typeof u.question=="string"&&typeof u.answer=="string");return A(c)}if(t.type==="Article"){const o=r.seo,c={headline:t.headline,datePublished:t.datePublished||(o==null?void 0:o.datePublished),dateModified:t.dateModified||(o==null?void 0:o.dateModified),author:t.author||(o==null?void 0:o.authorName),image:t.image||(o==null?void 0:o.ogImage)},u=E(c,n,a,null,i);if(!u)return null;if(r.template==="blog-post"){u["@type"]="BlogPosting",u.mainEntityOfPage={"@type":"WebPage","@id":n},c.image&&(u.image=[c.image]);const h=c.author;if(h){const l={"@type":"Person",name:h};o!=null&&o.authorUrl&&o.authorUrl.trim().length>0&&(l.url=o.authorUrl.trim()),u.author=l}o!=null&&o.articleSection&&(u.articleSection=o.articleSection),o!=null&&o.articleTags&&o.articleTags.length>0&&(u.keywords=o.articleTags.join(", "))}return u}if(t.type==="LocalBusiness")return null;if(t.type==="Breadcrumb"){const c=(t.items||[]).filter(h=>typeof h.name=="string"),u=typeof window<"u"?window.location.origin:"";return S(c,u)}return null}function z(){function t(n,i,a){const e=n.seo,d=window.location.origin,p=d+i,s=(e==null?void 0:e.title)||n.title;s&&(document.title=s),e!=null&&e.description&&m("name","description",e.description);const f=e!=null&&e.noindex?"noindex, nofollow":"index, follow";m("name","robots",f),m("property","og:title",(e==null?void 0:e.ogTitle)||(e==null?void 0:e.title)||n.title),m("property","og:description",(e==null?void 0:e.ogDescription)||(e==null?void 0:e.description)||""),m("property","og:url",p),m("property","og:type",n.template==="blog-post"?"article":"website");const o=w();o&&m("property","og:site_name",o),e!=null&&e.ogImage&&m("property","og:image",e.ogImage),m("name","twitter:card","summary_large_image"),m("name","twitter:title",(e==null?void 0:e.ogTitle)||(e==null?void 0:e.title)||n.title),m("name","twitter:description",(e==null?void 0:e.ogDescription)||(e==null?void 0:e.description)||"");const c=(e==null?void 0:e.twitterImage)||(e==null?void 0:e.ogImage)||"";m("name","twitter:image",c),n.template==="blog-post"?(e!=null&&e.datePublished?m("property","article:published_time",e.datePublished):document.querySelectorAll('meta[property="article:published_time"]').forEach(l=>l.remove()),e!=null&&e.dateModified?m("property","article:modified_time",e.dateModified):document.querySelectorAll('meta[property="article:modified_time"]').forEach(l=>l.remove()),e!=null&&e.authorName?(m("property","article:author",e.authorName),m("name","author",e.authorName)):(document.querySelectorAll('meta[property="article:author"]').forEach(l=>l.remove()),document.querySelectorAll('meta[name="author"]').forEach(l=>l.remove())),e!=null&&e.articleSection?m("property","article:section",e.articleSection):document.querySelectorAll('meta[property="article:section"]').forEach(l=>l.remove()),q("property","article:tag",(e==null?void 0:e.articleTags)??[])):(document.querySelectorAll('meta[property^="article:"]').forEach(l=>l.remove()),document.querySelectorAll('meta[name="author"]').forEach(l=>l.remove()));const u=e!=null&&e.canonical&&e.canonical.trim().length>0?P(e.canonical,d):"";j(u||p);const h=T(e==null?void 0:e.structuredData,n,p,d);if(y("page-schema",h),a!=null&&a.breadcrumb&&a.breadcrumb.length>0){const l=S(a.breadcrumb,d);y("breadcrumb",l)}else y("breadcrumb",null)}function r(){y("page-schema",null),y("breadcrumb",null),document.querySelectorAll('script[type="application/ld+json"][data-page]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-breadcrumb]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-video]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-faq]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-seo-key="faq"]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-team]').forEach(n=>n.remove()),document.querySelectorAll('script[type="application/ld+json"][data-seo-key="team"]').forEach(n=>n.remove())}return{setPageSEO:t,cleanupJsonLd:r,removeAllJsonLdBlocks:x}}export{A as b,z as u};