{"version":3,"file":"templates.mjs","names":[],"sources":["../../src/helpers/templates/editor/body.ts","../../src/helpers/templates/editor/index.ts","../../src/helpers/templates/index.ts"],"sourcesContent":["import type {\n  TemplateBody,\n  TemplateFilter,\n  TemplateBodyGrid,\n} from '~/modules/templates';\n\nimport {\n  createLayoutHelperFrom,\n  layoutHelperToJSON,\n  type LayoutHelper,\n} from './layouts';\n\nexport interface TemplateBodyHelper {\n  readonly version: number;\n  filters: Map<string, TemplateFilter>;\n  index?: string;\n  dateField: string;\n  layouts: LayoutHelper[];\n  grid?: TemplateBodyGrid;\n}\n\nexport function createTemplateBodyHelper(\n  index?: string,\n  dateField: string = '',\n  layouts: LayoutHelper[] = [],\n  filters: TemplateFilter[] = [],\n  grid?: TemplateBodyGrid\n): TemplateBodyHelper {\n  return {\n    version: 2,\n    dateField,\n    layouts,\n    filters: new Map(filters?.map((filter) => [filter.name, filter]) ?? []),\n    grid,\n    index,\n  };\n}\n\nexport function createTemplateBodyHelperFrom(\n  body: TemplateBody\n): TemplateBodyHelper {\n  return createTemplateBodyHelper(\n    body.index,\n    body.dateField,\n    body.layouts.map((lay) => createLayoutHelperFrom(lay)),\n    body.filters,\n    body.grid\n  );\n}\n\nexport function templateHelperBodyToJSON(\n  body: TemplateBodyHelper\n): TemplateBody {\n  return {\n    version: body.version,\n    dateField: body.dateField,\n    layouts: body.layouts.map((lay) => layoutHelperToJSON(lay)),\n    filters: Array.from(body.filters.values()),\n    grid: body.grid,\n    index: body.index,\n  };\n}\n\nexport function addLayoutOfHelper(\n  body: TemplateBodyHelper,\n  layout: LayoutHelper,\n  index?: number\n): TemplateBodyHelper {\n  if (body.layouts.some((lay) => lay.id === layout.id)) {\n    throw new Error(`Layout \"${layout.id}\" already exists`);\n  }\n  body.layouts.splice(index ?? body.layouts.length, 0, layout);\n  return body;\n}\n\nexport function removeLayoutOfHelper(\n  body: TemplateBodyHelper,\n  layout: LayoutHelper\n): TemplateBodyHelper {\n  const template = body;\n  template.layouts = body.layouts.filter((lay) => lay.id !== layout.id);\n  return body;\n}\n\nexport function updateLayoutOfHelper(\n  body: TemplateBodyHelper,\n  oldLayout: LayoutHelper,\n  newLayout: LayoutHelper\n): TemplateBodyHelper {\n  const index = body.layouts.findIndex((lay) => lay.id === oldLayout.id);\n  if (index < 0) {\n    throw new Error(`Layout \"${oldLayout.id}\" not found`);\n  }\n  const template = body;\n  template.layouts[index] = newLayout;\n  return body;\n}\n","import objectHash from 'object-hash';\n\nimport type {\n  InputTemplate,\n  Template,\n  TemplateLocale,\n} from '~/modules/templates';\n\nimport type { TemplateTagMap } from './tags';\nimport {\n  createTemplateBodyHelper,\n  createTemplateBodyHelperFrom,\n  templateHelperBodyToJSON,\n  type TemplateBodyHelper,\n} from './body';\n\nexport interface TemplateHelper {\n  readonly id: string;\n  name: string;\n  locale: 'fr' | 'en';\n  body: TemplateBodyHelper;\n  tags: TemplateTagMap;\n  readonly createdAt: Date;\n  readonly updatedAt?: Date;\n  readonly hash: string;\n}\n\nfunction hashTemplate(template: Template | TemplateHelper): string {\n  return objectHash({\n    name: template.name,\n    locale: template.locale,\n    body: template.body,\n    tags: template.tags,\n  });\n}\n\nexport function createTemplateHelper(\n  name: string = '',\n  body?: TemplateBodyHelper,\n  locale: TemplateLocale = 'en',\n  tags: TemplateTagMap = new Map(),\n  id: string = '',\n  createdAt: Date = new Date(),\n  updatedAt?: Date\n): TemplateHelper {\n  const template = {\n    id,\n    name,\n    locale,\n    body: body ?? createTemplateBodyHelper(),\n    tags,\n    createdAt,\n    updatedAt,\n    hash: '',\n  };\n\n  template.hash = hashTemplate(template);\n\n  return template;\n}\n\nexport function createTemplateHelperFrom(template: Template): TemplateHelper {\n  return createTemplateHelper(\n    template.name,\n    createTemplateBodyHelperFrom(template.body),\n    template.locale,\n    new Map(template.tags?.map((tag) => [tag.id, tag]) ?? []),\n    template.id,\n    template.createdAt,\n    template.updatedAt\n  );\n}\n\nexport function templateHelperToJSON(template: TemplateHelper): InputTemplate {\n  return {\n    name: template.name,\n    locale: template.locale,\n    body: templateHelperBodyToJSON(template.body),\n    tags: Array.from(template.tags.values()),\n  };\n}\n\nexport function hasTemplateChanged(template: TemplateHelper): boolean {\n  return template.hash !== hashTemplate(template);\n}\n\nexport {\n  type TemplateBodyHelper,\n  createTemplateBodyHelper,\n  createTemplateBodyHelperFrom,\n  addLayoutOfHelper,\n  removeLayoutOfHelper,\n  updateLayoutOfHelper,\n} from './body';\nexport * from './tags';\n","import { assignDependencies } from '~/helpers/permissions/decorator';\nimport {\n  type Template,\n  getTemplate,\n  upsertTemplate,\n} from '~/modules/templates';\n\nexport * from './editor';\n\n/**\n * Change visibility of a template\n *\n * @param templateOrId Template or Template's id\n * @param hidden New state\n *\n * @returns Updated template\n */\nexport async function changeTemplateVisibility(\n  templateOrId: Omit<Template, 'body'> | string,\n  hidden: boolean\n): Promise<Template> {\n  const base = await getTemplate(templateOrId);\n\n  const template = await upsertTemplate({\n    id: base.id,\n    name: base.name,\n    locale: base.locale,\n    tags: base.tags,\n    body: base.body,\n    hidden,\n  });\n\n  return template;\n}\nassignDependencies(changeTemplateVisibility, [getTemplate, upsertTemplate]);\n"],"mappings":";;;;;;;AAqBA,SAAgB,yBACd,OACA,YAAoB,IACpB,UAA0B,CAAC,GAC3B,UAA4B,CAAC,GAC7B,MACoB;;CACpB,OAAO;EACL,SAAS;EACT;EACA;EACA,SAAS,IAAI,sEAAI,QAAS,KAAK,WAAW,CAAC,OAAO,MAAM,MAAM,CAAC,wDAAK,CAAC,CAAC;EACtE;EACA;CACF;AACF;AAEA,SAAgB,6BACd,MACoB;CACpB,OAAO,yBACL,KAAK,OACL,KAAK,WACL,KAAK,QAAQ,KAAK,QAAQ,uBAAuB,GAAG,CAAC,GACrD,KAAK,SACL,KAAK,IACP;AACF;AAEA,SAAgB,yBACd,MACc;CACd,OAAO;EACL,SAAS,KAAK;EACd,WAAW,KAAK;EAChB,SAAS,KAAK,QAAQ,KAAK,QAAQ,mBAAmB,GAAG,CAAC;EAC1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