// Where a hosted DID's owner-gated control socket lives.
//
// Extracted because every operator command needs it and a second copy would drift: the PRIMARY DID keeps
// the FLAT <storage>/control.sock so its co-located clients (favela-web) saw no change across the
// single-DID -> multi-DID cutover, while every other hosted DID gets <storage>/dids/<did16>/control.sock.
// Getting this wrong looks exactly like "the daemon isn't running".
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
import { join } from 'bare-path'
const NS = (s) => b4a.from('atproto-base/v1/' + s)
// The 16-hex room-key prefix a DID's per-DID storage is filed under (matches lib/device-node.js).
export const did16For = (did) => b4a.toString(crypto.hash([NS('room-key'), b4a.from(did)]), 'hex').slice(0, 16)
export function sockFor (cfg, storage, did) {
const entry = (cfg.dids || []).find((d) => d.did === did)
return (entry && entry.primary) ? join(storage, 'control.sock') : join(storage, 'dids', did16For(did), 'control.sock')
}