// app-data.jsx — STAR 무대의상 실제 카탈로그 (data/products.json 기반)
// 가격은 '제작 문의' 방식(price=null). 색상은 맞춤제작 팔레트로 매핑.
const KRW = (n) => (n == null ? '제작 문의' : n.toLocaleString('ko-KR') + '원');

// 회사·연락처 정보 (출처: starevent.net) — 전 화면 공유
const CONTACT = {
  brand: 'STAR 무대의상',
  company: '(주)엠스타',
  ceo: '김정식',
  bizNo: '513-81-71747',
  sellNo: '제2014-경북칠곡-0091호',
  tel: '054-972-3741',
  fax: '054-974-0458',
  email: 'stevent@hanmail.net',
  addr: '경상북도 칠곡군 북삼읍 금오대로 81 (율리 636-10)',
  postal: '39805',
  hours: '평일 09:00~18:00 · 주말·공휴일 휴무',
  bank: '농협 355-0019-2208-13 (주)엠스타',
  kakao: 'https://pf.kakao.com/_HxkxfMG',          // 채널 홈
  kakaoChat: 'https://pf.kakao.com/_HxkxfMG/chat',  // 1:1 채팅 바로가기
  insta: 'https://www.instagram.com/hi.star_maker/',
  blog: 'https://blog.naver.com/kts4741',
};
if (typeof window !== 'undefined') window.CONTACT = CONTACT;
// 상품 딥링크 URL — 공유받은 사람이 이 옷을 바로 열 수 있게 ?p=<id>
const productUrl = (p) => location.origin + location.pathname + '?p=' + encodeURIComponent(p.sm || p.id);

// 색상 팔레트(무대의상 · 맞춤 제작 가능 색)
const COL = {
  red:     { name: '레드', hex: '#D7263D' },
  hotpink: { name: '핫핑크', hex: '#FF2E88' },
  pink:    { name: '핑크', hex: '#E84A8A' },
  gold:    { name: '골드', hex: '#E0A82E' },
  yellow:  { name: '옐로우', hex: '#F4C430' },
  emerald: { name: '에메랄드', hex: '#1F9E7A' },
  mint:    { name: '민트', hex: '#67C9A8' },
  royal:   { name: '로열블루', hex: '#1E3FB0' },
  sky:     { name: '스카이', hex: '#5EA9E6' },
  navy:    { name: '네이비', hex: '#23335E' },
  purple:  { name: '퍼플', hex: '#7A3FA0' },
  silver:  { name: '실버', hex: '#C5CBD3' },
  white:   { name: '화이트', hex: '#F4F1EA' },
  black:   { name: '블랙', hex: '#1A1714' },
};

// 한글 색상 토큰 → COL 키
const COLOR_TOKENS = [
  ['핫핑크', 'hotpink'], ['분홍', 'pink'], ['핑크', 'pink'], ['빨강', 'red'], ['레드', 'red'],
  ['금색', 'gold'], ['골드', 'gold'], ['노랑', 'yellow'], ['옐로', 'yellow'],
  ['민트', 'mint'], ['초록', 'emerald'], ['연두', 'emerald'], ['그린', 'emerald'],
  ['코발트', 'royal'], ['로열', 'royal'], ['하늘', 'sky'], ['하늘색', 'sky'], ['파랑', 'royal'], ['블루', 'royal'],
  ['남색', 'navy'], ['네이비', 'navy'], ['보라', 'purple'], ['퍼플', 'purple'],
  ['은색', 'silver'], ['실버', 'silver'], ['흰색', 'white'], ['하양', 'white'], ['화이트', 'white'], ['아이보리', 'white'],
  ['검정', 'black'], ['블랙', 'black'],
];
function colorsFor(korean) {
  const found = [];
  let s = korean || '';
  for (const [tok, key] of COLOR_TOKENS) {
    if (s.includes(tok) && !found.includes(key)) found.push(key);
  }
  // 제품 본연색 + 대표 맞춤색 팔레트
  const palette = ['red', 'hotpink', 'gold', 'royal', 'purple', 'black', 'white', 'emerald'];
  const merged = [...found];
  for (const k of palette) if (!merged.includes(k) && merged.length < 8) merged.push(k);
  return { primary: found[0] || null, list: merged.length ? merged : palette };
}

const SZ_DRESS = ['44', '55', '66', '77', '88', '자유(맞춤)'];
const SZ_TEE = ['S', 'M', 'L', 'XL', '2XL', '3XL'];
const SZ_HANBOK = ['소', '중', '대', '특대', '자유(맞춤)'];
const sizesFor = (type) => {
  if (['저고리', '쾌자', '치마', '원피스', '코트', '케이프'].includes(type)) return SZ_HANBOK;
  if (['셔츠', '바지', '탑', '조끼'].includes(type)) return SZ_TEE;
  return SZ_DRESS;
};

// data/products.json → CATALOG (window.__PRODUCTS 로 부트스트랩에서 주입)
const RAW = (typeof window !== 'undefined' && window.__PRODUCTS) || [];
const CATALOG = RAW.map((p) => {
  const c = colorsFor(p.color);
  // 갤러리: AI 연출 화보 → 모델 화보(starevent) → 실제착용샷
  const edG = p.editorial ? [{ src: p.editorial, label: 'AI 연출 화보', editorial: true }] : [];
  const modelG = (p.model || []).map((src, i) => ({ src, label: i === 0 ? '모델 화보' : `화보 ${i + 1}`, model: true }));
  const wornG = (p.gallery || []).map((g, i) => ({ src: g.src, label: g.label || (i === 0 ? '실제 의상' : `디테일 ${i}`) }));
  const gallery = [...edG, ...modelG, ...wornG];
  return {
    ...p,
    primaryColor: c.primary,
    colors: c.list,
    sizes: sizesFor(p.type),
    gallery,
    hasModel: modelG.length > 0,
    photos: gallery.map(g => g.label),
    img: p.editorial || (modelG[0] && modelG[0].src) || p.img,   // 대표: AI 연출 화보 → 모델 화보 → 정방형 썸네일(_t)
    tag: p.tag || tagFor(p),
  };
});

function tagFor(p) {
  if (p.genre === '트로트') return '트로트';
  if (p.genre && p.genre.includes('난타')) return '난타·장구';
  if (p.genre === '합창' || p.genre === '워쉽') return p.genre;
  return null;
}

// 카테고리(실제 보유) — categories.json
// 카테고리 대표 연출컷(사진 칩) — 에디토리얼 화보 중 선정
const CAT_CHIP = {
  '저고리': 'assets/editorial/SM-0022_e.jpg',
  '치마': 'assets/editorial/SM-0016_e.jpg',
  '원피스': 'assets/editorial/SM-0001_e.jpg',
  '쾌자': 'assets/editorial/SM-0113_e.jpg',
  '자켓': 'assets/editorial/SM-0102_e.jpg',
  '조끼': 'assets/editorial/SM-0003_e.jpg',
  '블라우스': 'assets/editorial/SM-0262_e.jpg',
  '셔츠': 'assets/editorial/SM-1146_e.jpg',
  '바지': 'assets/editorial/SM-0083_e.jpg',
  '드레스': 'assets/editorial/SM-0169_e.jpg',
  '탑': 'assets/editorial/SM-0040_e.jpg',
  '코트': 'assets/editorial/SM-0536_e.jpg',
  '케이프': 'assets/editorial/SM-0638_e.jpg',
  '액세서리': 'assets/editorial/SM-1734_e.jpg',
};
const CATS = ((typeof window !== 'undefined' && window.__CATEGORIES) || []).map(c => ({
  name: c.name, emoji: c.emoji, count: c.count, chip: CAT_CHIP[c.name] || null,
}));

// 후기(샘플 · 무대의상 톤)
const REVIEWS = [
  { name: '이○○', stars: 5, cat: '원피스', text: '무대 올라가니까 조명에 반짝반짝 너무 예뻤어요! 실측대로 맞춰주셔서 사이즈도 딱 맞았어요.', size: '66 / 레드' },
  { name: '박○○', stars: 5, cat: '단체', text: '8명 단체로 맞췄는데 카톡으로 사이즈 정리해서 보내니 일사천리였어요. 다음 공연도 여기서 합니다.', size: 'L 외 / 블랙' },
  { name: '최○○', stars: 4, cat: '쾌자', text: '활동이 편해서 장구 칠 때 좋아요. 색감도 화면이랑 똑같이 왔어요.', size: '중 / 골드' },
];

Object.assign(window, { KRW, COL, CATALOG, CATS, REVIEWS, SZ_DRESS, SZ_TEE, SZ_HANBOK, colorsFor });
