File size: 4,963 Bytes
ed33547
 
 
 
 
 
 
 
 
 
 
25058c7
 
ed33547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15a4294
ed33547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25058c7
 
 
 
 
 
 
 
 
 
 
 
 
ed33547
25058c7
ed33547
 
 
 
25058c7
 
 
 
 
 
 
ed33547
 
25058c7
ed33547
 
25058c7
 
 
 
ed33547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type { JobsheetSection, Page } from "../types/session";

export type FlatPage = {
  sectionId: string;
  sectionIndex: number;
  sectionTitle?: string;
  pageIndex: number;
  page: Page;
  flatIndex: number;
};

const MAX_PHOTOS_PRIMARY_PAGE = 2;
const MAX_PHOTOS_CONTINUATION_PAGE = 6;

function cloneTemplate(template?: Page["template"]): Page["template"] {
  if (!template) return undefined;
  return { ...template };
}

function normalizePhotoIds(photoIds?: string[]): string[] {
  if (!Array.isArray(photoIds)) return [];
  return photoIds.filter((value) => Boolean(value));
}

function buildPhotoContinuation(source: Page, photoIds: string[]): Page {
  return {
    items: [],
    template: cloneTemplate(source.template),
    photo_ids: photoIds,
    photo_layout: source.photo_layout,
    photo_order_locked: source.photo_order_locked,
    variant: "photos",
  };
}

function splitPagePhotos(page: Page): Page[] {
  const items = Array.isArray(page.items) ? page.items : [];
  const normalized: Page = { ...page, items };

  if (normalized.blank) {
    return [normalized];
  }

  const photoIds = normalizePhotoIds(normalized.photo_ids);
  if (!photoIds.length) {
    return [normalized];
  }

  if (normalized.variant === "photos") {
    const chunks: string[][] = [];
    for (let i = 0; i < photoIds.length; i += MAX_PHOTOS_CONTINUATION_PAGE) {
      chunks.push(photoIds.slice(i, i + MAX_PHOTOS_CONTINUATION_PAGE));
    }
    if (chunks.length <= 1) {
      return [
        {
          ...normalized,
          photo_ids: chunks[0] ?? [],
          variant: "photos",
        },
      ];
    }
    return chunks.map((chunk, idx) => {
      if (idx === 0) return { ...normalized, photo_ids: chunk, variant: "photos" };
      return buildPhotoContinuation(normalized, chunk);
    });
  }

  const baseChunk = photoIds.slice(0, MAX_PHOTOS_PRIMARY_PAGE);
  const extraIds = photoIds.slice(MAX_PHOTOS_PRIMARY_PAGE);
  const continuationChunks: string[][] = [];
  for (let i = 0; i < extraIds.length; i += MAX_PHOTOS_CONTINUATION_PAGE) {
    continuationChunks.push(extraIds.slice(i, i + MAX_PHOTOS_CONTINUATION_PAGE));
  }

  const basePage: Page = {
    ...normalized,
    photo_ids: baseChunk,
    variant: normalized.variant ?? "full",
  };
  if (!continuationChunks.length) {
    return [basePage];
  }
  const extraPages = continuationChunks.map((chunk) =>
    buildPhotoContinuation(normalized, chunk),
  );
  return [basePage, ...extraPages];
}

export function ensureSections(sections?: JobsheetSection[]): JobsheetSection[] {
  if (sections && sections.length) {
    return sections.map((section, index) => {
      const basePages = section.pages?.length ? section.pages : [{ items: [] }];
      const normalizedPages = basePages.flatMap((page) => splitPagePhotos(page));
      return {
        id: section.id,
        title: section.title ?? "Section",
        pages: normalizedPages.length ? normalizedPages : [{ items: [] }],
      };
    });
  }
  return [
    {
      id: crypto.randomUUID(),
      title: "Section 1",
      pages: [{ items: [] }],
    },
  ];
}

export function flattenSections(sections: JobsheetSection[]): FlatPage[] {
  const result: FlatPage[] = [];
  sections.forEach((section, sectionIndex) => {
    const pages = section.pages ?? [];
    pages.forEach((page, pageIndex) => {
      result.push({
        sectionId: section.id,
        sectionIndex,
        sectionTitle: section.title ?? `Section ${sectionIndex + 1}`,
        pageIndex,
        page,
        flatIndex: result.length,
      });
    });
  });
  return result;
}

export function replacePage(
  sections: JobsheetSection[],
  sectionIndex: number,
  pageIndex: number,
  nextPage: Page,
): JobsheetSection[] {
  const next = sections.map((section, sIdx) => {
    if (sIdx !== sectionIndex) return section;
    const nextPages = [...(section.pages ?? [])];
    nextPages[pageIndex] = nextPage;
    return { ...section, pages: nextPages };
  });
  return ensureSections(next);
}

export function insertPage(
  sections: JobsheetSection[],
  sectionIndex: number,
  pageIndex: number,
  page: Page,
): JobsheetSection[] {
  const next = sections.map((section, sIdx) => {
    if (sIdx !== sectionIndex) return section;
    const nextPages = [...(section.pages ?? [])];
    const safeIndex = Math.max(0, Math.min(pageIndex, nextPages.length));
    nextPages.splice(safeIndex, 0, page);
    return { ...section, pages: nextPages };
  });
  return ensureSections(next);
}

export function removePage(
  sections: JobsheetSection[],
  sectionIndex: number,
  pageIndex: number,
): JobsheetSection[] {
  const next = sections.map((section, sIdx) => {
    if (sIdx !== sectionIndex) return section;
    const nextPages = [...(section.pages ?? [])];
    if (nextPages.length <= 1) return section;
    nextPages.splice(pageIndex, 1);
    return { ...section, pages: nextPages.length ? nextPages : [{ items: [] }] };
  });
  return ensureSections(next);
}