import type {DocArticle,DocGroup,DocStatus} from './types';
export const docsUpdated = '۲۰۲۶/۰۹/۲۴ · ۲ مهر ۱۴۰۵';
export const docGroups: {id:DocGroup;title:string}[] = [
  {id:'start',title:'شروع آشنایی'},
  {id:'marketplace',title:'بازار و سرمایه‌گذاری'},
  {id:'tokenization',title:'توکنایزیشن دارایی'},
  {id:'account',title:'حساب و کیف پول'},
  {id:'admin',title:'کنسول مدیریت'},
  {id:'network',title:'شبکه، نود و ولیدیتور'},
  {id:'security',title:'امنیت و اعتماد'},
  {id:'api',title:'مرجع API'},
  {id:'roadmap',title:'تاریخچه و آینده'},
];
export const docStatusLabels:Record<DocStatus,string> = {
  implemented:'پیاده‌سازی‌شده',configured:'نیازمند پیکربندی',preview:'پیش‌نمایش',planned:'برنامهٔ آینده',reference:'مرجع',
};

export type DocSearchEntry = Pick<DocArticle,'slug'|'title'|'description'|'group'|'status'> & {text:string};
export const docHref = (slug:string)=>slug==='welcome'?'/docs':`/docs/${slug}`;

export function normalizeDocSearch(value:string){
  return value.toLocaleLowerCase().replace(/[يى]/g,'ی').replace(/ولیدیتور|validator(?:s)?/g,'اعتبارسنج').replace(/ك/g,'ک').replace(/[\u064B-\u065F\u0670]/g,'').replace(/[\u200c\u200d]/g,' ').replace(/\s+/g,' ').trim();
}
export function searchDocs(entries:DocSearchEntry[],query:string){
  const terms=normalizeDocSearch(query).split(' ').filter(Boolean);
  if(!terms.length)return [];
  return entries.map(entry=>{
    const title=normalizeDocSearch(entry.title),description=normalizeDocSearch(entry.description),body=normalizeDocSearch(entry.text),slug=entry.slug.toLowerCase();
    const score=terms.reduce((sum,term)=>sum+(title.includes(term)?12:description.includes(term)?5:slug.includes(term)?4:body.includes(term)?1:0),0);
    const matches=terms.every(term=>`${title} ${description} ${body} ${slug}`.includes(term));
    return {entry,score:matches?score:0};
  }).filter(result=>result.score>0).sort((a,b)=>b.score-a.score).slice(0,24).map(result=>result.entry);
}

export function docMarkdown(article:DocArticle){
  const lines=[`# ${article.title}`,'',article.description,'',`وضعیت: ${docStatusLabels[article.status]} | مخاطب: ${article.audience}`,`بازبینی مستندات: ${docsUpdated}`,''];
  for(const section of article.sections){
    lines.push(`## ${section.title}`,'',...(section.paragraphs||[]).flatMap(text=>[text,'']));
    if(section.bullets)lines.push(...section.bullets.map(text=>`- ${text}`),'');
    if(section.table){
      const cell=(text:string)=>text.replace(/\|/g,'\\|').replace(/\n/g,' ');
      lines.push(`| ${section.table.headers.map(cell).join(' | ')} |`,`| ${section.table.headers.map(()=>'---').join(' | ')} |`,...section.table.rows.map(row=>`| ${row.map(cell).join(' | ')} |`),'');
    }
    if(section.code)lines.push('```'+section.code.language.toLowerCase(),section.code.content,'```','');
    if(section.note)lines.push(`> ${section.note.title}: ${section.note.text}`,'');
    if(section.links)lines.push(...section.links.map(link=>`- [${link.label}](${link.href})`),'');
  }
  lines.push('## مراجع پیاده‌سازی','',...article.sources.map(source=>`- ${source}`),'');
  return lines.join('\n');
}
