import { type Result } from "@shared/utils/result.ts";
import {
    type InferSchema,
    InferSchemaType,
    Schema,
} from "@shared/utils/validator.ts";

export type ExtractRouteParams<T extends string> = T extends string
    ? T extends `${infer _Start}:${infer Param}/${infer Rest}`
        ? Param | ExtractRouteParams<Rest>
    : T extends `${infer _Start}:${infer Param}` ? Param
    : never
    : never;

class ClientApi<
    Path extends string,
    ReqSchema extends Schema<any>,
    ResSchema extends Schema<any>,
> {
    private readonly path: string[];
    private readonly paramsIndexes: Record<string, number>;

    constructor(
        path: Path,
        public readonly reqSchema: ReqSchema,
        public readonly resSchema: ResSchema,
    ) {
        this.path = path.split("/");
        this.paramsIndexes = this.path.reduce<Record<number, string>>(
            (acc, segment, index) => {
                if (segment.startsWith(":")) {
                    acc[index] = segment.slice(1);
                }
                return acc;
            },
            {},
        );
    }

    makeRequest(
        reqBody: InferSchemaType<ReqSchema>,
        params?: { [K in ExtractRouteParams<Path>]: string },
    ) {
        const path = this.path.slice().reduce<string>((acc, cur) => {});
        if (params) {
            for (const param of Object.keys(params)) {
                pathSplitted[this.paramsIndexes[param]] = param;
            }
        }
    }
}
