const g=new Set([!0,!1,"alt","title"]);function u(s,t){return(Array.isArray(s)?s:[]).filter(([e])=>e!==t)}function d(s,t){s&&s.attrs&&(s.attrs=u(s.attrs,t))}function y(s,t){if(!g.has(s))throw new TypeError(`figcaption must be one of: ${[...g]}.`);if(s==="alt")return t.content;const e=t.attrs.find(([c])=>c==="title");return Array.isArray(e)&&e[1]?(d(t,"title"),e[1]):void 0}function k(s,t){t=t||{},s.core.ruler.before("linkify","image_figures",function(e){let c=1;for(let o=1,p=e.tokens.length;o<p-1;++o){const r=e.tokens[o];if(r.type!=="inline"||!r.children||r.children.length!==1&&r.children.length!==3||r.children.length===1&&r.children[0].type!=="image")continue;if(r.children.length===3){const[i,a,f]=r.children;if(i.type!=="link_open"||a.type!=="image"||f.type!=="link_close")continue}if(o!==0&&e.tokens[o-1].type!=="paragraph_open"||o!==p-1&&e.tokens[o+1].type!=="paragraph_close")continue;const l=e.tokens[o-1];let n;if(l.type="figure_open",l.tag="figure",e.tokens[o+1].type="figure_close",e.tokens[o+1].tag="figure",t.dataType&&e.tokens[o-1].attrPush(["data-type","image"]),t.link&&r.children.length===1){[n]=r.children;const i=new e.Token("link_open","a",1);i.attrPush(["href",n.attrGet("src")]),r.children.unshift(i),r.children.push(new e.Token("link_close","a",-1))}if(n=r.children.length===1?r.children[0]:r.children[1],t.figcaption){const i=y(t.figcaption,n);if(i){const[a]=s.parseInline(i,e.env);r.children.push(new e.Token("figcaption_open","figcaption",1)),r.children.push(...a.children),r.children.push(new e.Token("figcaption_close","figcaption",-1)),n.attrs&&(n.attrs=u(n.attrs,"title"))}}if(t.copyAttrs&&n.attrs){const i=t.copyAttrs===!0?"":t.copyAttrs;l.attrs=n.attrs.filter(([a])=>a.match(i)).map(a=>Array.from(a))}if(t.tabindex&&(e.tokens[o-1].attrPush(["tabindex",c]),c++),t.lazy&&(n.attrs.some(([i])=>i==="loading")||n.attrs.push(["loading","lazy"])),t.async&&(n.attrs.some(([i])=>i==="decoding")||n.attrs.push(["decoding","async"])),t.classes&&typeof t.classes=="string"){let i=!1;for(let a=0,f=n.attrs.length;a<f&&!i;a++){const h=n.attrs[a];h[0]==="class"&&(h[1]=`${h[1]} ${t.classes}`,i=!0)}i||n.attrs.push(["class",t.classes])}if(t.removeSrc){const i=n.attrs.find(([a])=>a==="src");n.attrs.push(["data-src",i[1]]),d(n,"src")}}})}export{k as r};
