/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { execFile } from 'node:child_process';
import { Messages } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { startBridgeServer } from '../../server/index.js';
import { PLUGIN_VERSION } from '../../version.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.serve');
export default class Serve extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        port: Flags.integer({
            char: 'p',
            summary: messages.getMessage('flags.port.summary'),
            default: 4195,
        }),
        hostname: Flags.string({
            char: 'H',
            summary: messages.getMessage('flags.hostname.summary'),
            default: '127.0.0.1',
        }),
        open: Flags.boolean({
            summary: messages.getMessage('flags.open.summary'),
            default: false,
        }),
    };
    async run() {
        const { flags } = await this.parse(Serve);
        const cwd = process.cwd();
        const { port: requestedPort, hostname, open } = flags;
        const { port } = await startBridgeServer({ cwd, port: requestedPort, hostname });
        if (port !== requestedPort) {
            this.log(`Port ${requestedPort} in use, using ${port} instead.`);
        }
        const url = `http://${hostname}:${port}`;
        this.log(messages.getMessage('info.started', [url, cwd]));
        if (open) {
            const PLATFORM_OPENER = { darwin: 'open', win32: 'start' };
            const cmd = PLATFORM_OPENER[process.platform] ?? 'xdg-open';
            execFile(cmd, [url]);
        }
        await new Promise(() => { });
        return { port, hostname, cwd, version: PLUGIN_VERSION };
    }
}
//# sourceMappingURL=serve.js.map