import { requestClient } from '#/api/request';

import dayjs from 'dayjs';

const prefix = 'reconciliation/';

function formatSearchTimeParam(st: unknown): [string, string] | undefined {
  if (!Array.isArray(st) || st.length < 2) return undefined;
  const fmt = (v: unknown) => {
    if (v && typeof v === 'object' && 'format' in v && typeof (v as { format: (s: string) => string }).format === 'function') {
      return (v as dayjs.Dayjs).format('YYYY-MM-DD');
    }
    const d = dayjs(v as string | number);
    return d.isValid() ? d.format('YYYY-MM-DD') : '';
  };
  const a = fmt(st[0]);
  const b = fmt(st[1]);
  if (!a || !b) return undefined;
  return [a, b];
}

/**
 * 诊所开票价格（部门财务口径）
 */
export async function getInvoicePriceApi(params: {
  store_id: number;
  search_time: unknown;
}) {
  const search_time = formatSearchTimeParam(params.search_time);
  if (!search_time) {
    return Promise.reject(new Error('时间范围无效'));
  }
  return requestClient.get<any>(`${prefix}invoice-price`, {
    params: {
      store_id: params.store_id,
      search_time,
    },
  });
}
