/**
 * シルギア 祝日・営業日API v1 の型定義
 * https://api.shirugear.com/holidays/
 *
 * 互換の方針: v1 の中では項目を「足す」ことはあっても、名前や意味は変えません。
 * 知らない項目（kind の新しい値を含む）が増えていても無視してください。
 */

/** national=国民の祝日 / substitute=振替休日 / citizens=国民の休日 / special=その年だけの休日（即位の礼など） */
export type HolidayKind = "national" | "substitute" | "citizens" | "special";

export interface Holiday {
  /** 日付 "YYYY-MM-DD"（日本の日付） */
  date: string;
  weekday: "日" | "月" | "火" | "水" | "木" | "金" | "土";
  /** 名前。振替休日・国民の休日は見分けた名前（"振替休日" / "国民の休日"） */
  name: string;
  kind: HolidayKind;
  /** 内閣府の一覧での書き方（振替休日・国民の休日は "休日"） */
  official_name: string;
}

/** GET /holidays/v1/{西暦4桁}.json（1955〜2100年） */
export interface HolidaysYearResponse {
  year: number;
  /** true=内閣府が公表済みの確定した年 / false=今の祝日法で計算した予定 */
  confirmed: boolean;
  /** 日付の順 */
  holidays: Holiday[];
  /** 確定している最後の年 */
  confirmed_through: number;
  /** 内閣府の一覧が変わったのを取り込んだ日 "YYYY-MM-DD" */
  data_version: string;
}

/** GET /holidays/v1/all.json */
export interface HolidaysAllResponse {
  from: number;
  to: number;
  confirmed_through: number;
  /** キーは日付 "YYYY-MM-DD" */
  holidays: Record<string, { name: string; kind: HolidayKind }>;
  data_version: string;
}

/** GET /holidays/v1/meta.json */
export interface HolidaysMetaResponse {
  data_version: string;
  /** データを作った日時（ISO 8601・UTC） */
  built_at: string;
  from: number;
  to: number;
  confirmed_through: number;
  sources: { holidays: string; astronomy: string };
  counts: {
    /** 全期間の祝日・休日の数 */
    holidays: number;
    /** そのうち確定の数 */
    confirmed: number;
  };
}

/** エラーのときの本文 */
export interface ErrorResponse {
  error: {
    status: 400 | 404 | 405;
    code: "invalid_year" | "year_not_found" | "not_found" | "method_not_allowed" | "invalid_request";
    message: string;
  };
}

/* ---- /holidays/v1/holidays.js（営業日の計算） ---- */

/** 日付は "YYYY-MM-DD" か Date（その端末の日付として読む） */
export type DateInput = string | Date;

export interface CalendarOptions {
  /** 休みの曜日（0=日 … 6=土）。既定 [0, 6] */
  closedWeekdays?: number[];
  /** 祝日・振替休日・国民の休日を休みにする。既定 true */
  holidays?: boolean;
  /** 年末年始の休み。"bank" は 12/31〜1/3。既定 false */
  yearEnd?: false | "bank" | { from: string; to: string };
  /** ほかに休みにする日 */
  closedDates?: string[];
  /** 休みの日でも営業する日（ほかのどの指定よりも優先） */
  openDates?: string[];
}

export declare class BusinessCalendar {
  readonly from: number;
  readonly to: number;
  readonly confirmedThrough: number;
  readonly dataVersion: string;
  holiday(date: DateInput): { date: string; name: string; kind: HolidayKind } | null;
  isHoliday(date: DateInput): boolean;
  isConfirmed(date: DateInput): boolean;
  isBusinessDay(date: DateInput): boolean;
  /** n 営業日後（n が負なら前）。その日自身は数えない */
  addBusinessDays(date: DateInput, n: number): string;
  /** 休みなら次（前）の営業日へ。営業日ならそのまま */
  adjust(date: DateInput, direction?: "next" | "previous"): string;
  /** 両端を含む */
  countBusinessDays(from: DateInput, to: DateInput): number;
  businessDaysInMonth(year: number, month: number): string[];
  lastBusinessDayOfMonth(year: number, month: number): string | null;
}

export declare function loadCalendar(options?: CalendarOptions): Promise<BusinessCalendar>;
