// Where this copy came from, and so where its updates come from:
// web (the website), bazaar, myket, direct (the website APK), unknown (e.g. iOS for now).
// A new store: a flavor in android/app/build.gradle, an entry here, its channel in public/app-update.json.
export type DistributionChannel = "web" | "bazaar" | "myket" | "direct" | "unknown";

const KNOWN_CHANNELS: readonly DistributionChannel[] = ["web", "bazaar", "myket", "direct", "unknown"];

export function toChannel(value: unknown): DistributionChannel {
    return KNOWN_CHANNELS.includes(value as DistributionChannel)
        ? (value as DistributionChannel)
        : "unknown";
}

/** How to open this app's page in a store app, and the web page if the store app is missing. */
export interface StoreLink {
    url: string;
    /** Android package to pin the intent to, so the store opens rather than a chooser. */
    package?: string;
    fallbackUrl?: string;
}

/** Each store's documented deep link; the update manifest can override it. */
export function defaultStoreLink(channel: DistributionChannel, appId: string): StoreLink | null {
    const id = encodeURIComponent(appId);

    switch (channel) {
        case "bazaar":
            return {
                url: `bazaar://details?id=${id}`,
                package: "com.farsitel.bazaar",
                fallbackUrl: `https://cafebazaar.ir/app/${id}`,
            };
        case "myket":
            return {
                url: `myket://details?id=${id}`,
                package: "ir.mservices.market",
                fallbackUrl: `https://myket.ir/app/${id}`,
            };
        default:
            // "direct" has no store: its link is the APK URL from the manifest.
            return null;
    }
}

/** Persian name of the channel, for UI copy ("دریافت از بازار"). */
export function channelLabel(channel: DistributionChannel): string {
    switch (channel) {
        case "bazaar":
            return "بازار";
        case "myket":
            return "مایکت";
        case "direct":
            return "وب‌سایت رنگوک";
        default:
            return "";
    }
}
