const E={1:"Протокол в свободной форме",2:"Протокол с подпунктами",3:"Протокол терапевта (МедВет)",4:"Узи-протокол (МедВет)"};function g(e){const t=Number(e);return t===2||t===3||t===4?t:1}function N(e){const t=String(e??"").trim(),r=/^builtin-default-v(\d+)$/i.exec(t);return r?g(Number(r[1])):null}function z(e){const t=N(e.protocolKey),r=g(e.agentVersion);return t!=null?Math.max(t,r):r}function L(e){return e===2||e===3||e===4}function j(e){return e===1||e===2}function _(e){return E[e]??"Протокол"}function H(e){const t=String(e.title??"").trim();return t&&!/^Системный протокол v\d+$/i.test(t)?t:_(g(e.agentVersion))}const C=new Set(["","н/д","нд","n/a","na","-","—","–"]),R=/^(\d+)\.(\d+)\.\s*(.+?):\s*(.*)$/;function A(e){return e.trim().toLowerCase().replace(/\s+/g," ").replace(/\.+$/g,"")}function a(e){return C.has(A(e))}function w(e){return e.trim().replace(/\s*\([^)]*\)/g,"").replace(/\s+/g," ")}function h(e){const t=[];for(const r of e.split(`
`)){const n=r.trim();if(!n)continue;const o=R.exec(n);if(!o){t.push({index:"",label:"",value:n});continue}t.push({index:`${o[1]}.${o[2]}.`,label:o[3].trim(),value:o[4].trim()})}return t}function f(e,t){const n=e[t];return typeof n=="string"?n.trim():""}function O(e){const t=e.lastIndexOf(":");if(t<0)return!1;const r=e.slice(t+1).trim();return r.length>0&&!a(r)}function c(e,t){const r=e.trim();return r?t?/^\d+\.\d+\./m.test(r)?r.split(`
`).some(O):h(r).some(o=>o.value.trim()&&!a(o.value))?!0:!a(r):!a(r):!1}function S(e,t,r,n=[]){const o=[t,...n];for(const i of o){const u=f(e,i);if(c(u,r))return u}return f(e,t)}function q(e,t,r){const n=L(t);if(t===3||t===4)return r?r.sections.some(i=>c(f(e,i.extractField),!0)):!1;const o=c(f(e,"diagnostics"),n)||c(S(e,n?"treatment":"treatment_plan",n,n?["treatment_plan"]:["recommendations"]),n);return c(S(e,"anamnesis",n,["complaints"]),n)||c(f(e,"examination_results"),n)||o||c(f(e,"diagnosis"),!1)}function d(e){const t=(e==null?void 0:e.trim())??"";return a(t)?"":t}function V(e){return e.filter(t=>(t.label,!!d(t.value)))}function v(e){var t;if(e.mode==="flat"){const r=d(e.flatText);return r?{...e,flatText:r}:null}if(e.mode==="structured"&&((t=e.subfields)!=null&&t.length)){const r=V(e.subfields);return r.length?{...e,subfields:r}:null}return null}function F(e){return e.map(t=>v(t)).filter(t=>t!==null)}function P(e,t){const r=e.trim();if(!r)return"";const n=/^(\d+)\.(\d+)\.$/.exec(r);if(!n)return r;const[,o,i]=n,u=(t==null?void 0:t.hideSectionNumbers)===!0,l=(t==null?void 0:t.hideSubpointNumbers)===!0;return u&&l?"":u?`${i}.`:l?`${o}.`:r}function k(e,t){var n;const r=v(e);return r?r.mode==="flat"&&r.flatText?r.flatText.trim():r.mode==="structured"&&((n=r.subfields)!=null&&n.length)?r.subfields.map(o=>{const i=d(o.value);if(!i)return"";if(o.label){const u=P(o.index,t);return`${u?`${u} `:""}${o.label}: ${i}`.trim()}return i}).filter(Boolean).join(`
`):"":""}function K(e,t){const r=(t==null?void 0:t.sectionSeparator)??`

`;return F(e).map(n=>{const o=k(n,t);return o.trim()?`${n.title}
${o}`:""}).filter(Boolean).join(r)}function m(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function p(e){return e.trim().toLowerCase().replace(/\s+/g," ").replace(/\.+$/,"")}function x(e){const t=e.trim();if(!t)return"";const r=t.split(`
`).map(n=>m(n.trim())).filter(Boolean);return r.length?`<p>${r.join("<br>")}</p>`:""}function I(e){const t=h(e).filter(r=>(!r.label&&r.value||r.value.length>0)&&!a(r.value));return t.length?$(t):x(e)}function $(e){const t=[];for(const r of e)r.label?t.push(`<strong>${m(r.label)}</strong>: ${m(r.value)}`):r.value&&t.push(m(r.value));return t.length?`<p>${t.join("<br>")}</p>`:""}const T=/<strong[^>]*>(.*?)<\/strong>\s*:\s*([^<]*)/gi;function B(e){var n;const t=new Map;if(!e)return t;const r=e.replace(/<br\s*\/?>/gi,`
`);for(const o of r.split(`
`)){const i=o.trim();if(!i)continue;T.lastIndex=0;const u=T.exec(i);u&&t.set(p(b(u[1])),b(u[2]).trim())}if(t.size===0){const o=new DOMParser().parseFromString(e,"text/html");for(const i of Array.from(o.querySelectorAll("strong"))){const u=((n=i.textContent)==null?void 0:n.trim())??"";if(!u)continue;let l="",s=i.nextSibling;for(;s;){if(s.nodeType===Node.TEXT_NODE)l+=s.textContent??"";else{if(s.nodeType===Node.ELEMENT_NODE&&s.tagName==="BR")break;s.nodeType===Node.ELEMENT_NODE&&(l+=s.textContent??"")}s=s.nextSibling}l=l.replace(/^:\s*/,"").trim(),u&&t.set(p(u),l)}}return t}function b(e){return e.replace(/<[^>]+>/g,"").replace(/\u00a0/g," ").trim()}function D(e,t){const r=h(t).filter(i=>i.label&&i.value&&!a(i.value));if(!r.length)return e;const n=B(e);for(const i of r)n.set(p(i.label),i.value);const o=Array.from(n.entries()).map(([i,u])=>({index:"",label:i,value:u}));return $(o)}function M(e,t){return t?!e||y(e)?t:`${e}<hr><p></p>${t}`:e}function y(e){return!b(e.replace(/<br\s*\/?>/gi,`
`))}function U(e,t,r){const n=r?I(t):x(t);return n?y(e)?n:r?D(e,t):M(e,n):e}export{x as a,K as b,k as c,V as d,q as e,F as f,f as g,L as h,a as i,w as j,z as k,H as l,U as m,g as n,v as o,I as p,j as q,S as r,c as s};
