(function (){
'use strict';
const TOPO_URL='https://cdn.jsdelivr.net/npm/us-atlas@3/counties-10m.json';
let topoCache=null;
function loadTopo(){
if(topoCache) return Promise.resolve(topoCache);
return d3.json(TOPO_URL).then(function (us){
topoCache=us;
return us;
});
}
function renderMap(el){
const abbr=el.dataset.state;
const stateFips=parseInt(el.dataset.fips, 10);
const height=parseInt(el.dataset.height, 10)||520;
const cfg=(window.bcCountyMaps||{})[abbr];
if(!cfg) return;
const upColor=cfg.upColor||'#152330';
const hoverColor=cfg.hoverColor||'#f8ad90';
const borderColor=cfg.borderColor||'#ffffff';
const countyData=cfg.counties||{};
loadTopo().then(function (us){
const allCounties=topojson.feature(us, us.objects.counties);
const stateFeatures=allCounties.features.filter(function (f){
return Math.floor(parseInt(f.id, 10) / 1000)===stateFips;
});
if(!stateFeatures.length) return;
const stateGeo={ type: 'FeatureCollection', features: stateFeatures };
const width=el.offsetWidth||700;
const projection=d3.geoAlbers().fitSize([width, height], stateGeo);
const path=d3.geoPath(projection);
const svg=d3.select(el)
.append('svg')
.attr('viewBox', '0 0 ' + width + ' ' + height)
.attr('width',  '100%')
.attr('height', height)
.attr('role',   'img')
.attr('aria-label', el.getAttribute('aria-label')||'County map');
const tooltip=d3.select('body')
.append('div')
.attr('class', 'bc-map-tooltip')
.style('opacity', 0);
svg.selectAll('.bc-county')
.data(stateFeatures)
.enter()
.append('path')
.attr('class', 'bc-county')
.attr('d', path)
.attr('fill', function (d){
const fips=String(d.id).padStart(5, '0');
return (countyData[fips]&&countyData[fips].url) ? upColor:'#c8d0d8';
})
.attr('stroke', borderColor)
.attr('stroke-width', 0.6)
.attr('cursor', function (d){
const fips=String(d.id).padStart(5, '0');
return (countyData[fips]&&countyData[fips].url) ? 'pointer':'default';
})
.on('mouseenter', function (event, d){
const fips=String(d.id).padStart(5, '0');
const county=countyData[fips]||{};
if(county.url) d3.select(this).attr('fill', hoverColor);
tooltip
.style('opacity', 1)
.html('<strong>' + (county.name||fips) + '</strong>' + (county.url ? '<br><small>Click to search property records</small>':''))
.style('left', (event.pageX + 14) + 'px')
.style('top',  (event.pageY - 36) + 'px');
})
.on('mousemove', function (event){
tooltip
.style('left', (event.pageX + 14) + 'px')
.style('top',  (event.pageY - 36) + 'px');
})
.on('mouseleave', function (event, d){
const fips=String(d.id).padStart(5, '0');
const county=countyData[fips]||{};
d3.select(this).attr('fill', (county.url) ? upColor:'#c8d0d8');
tooltip.style('opacity', 0);
})
.on('click', function (event, d){
const fips=String(d.id).padStart(5, '0');
const county=countyData[fips]||{};
if(county.url) window.open(county.url, '_blank', 'noopener');
});
svg.selectAll('.bc-county-label')
.data(stateFeatures)
.enter()
.append('text')
.attr('class', 'bc-county-label')
.attr('transform', function (d){ return 'translate(' + path.centroid(d) + ')'; })
.attr('dy', '0.35em')
.style('display', function (d){
const b=path.bounds(d);
const w=b[1][0] - b[0][0];
return w > 28 ? null:'none';
})
.text(function (d){
const fips=String(d.id).padStart(5, '0');
const county=countyData[fips]||{};
const name=county.name||'';
const b=path.bounds(d);
const w=b[1][0] - b[0][0];
return w < 50 ? name.split(' ')[0]:name;
});
}).catch(function (err){
el.innerHTML='<p style="color:#c00;padding:16px">Failed to load map data. Check console.</p>';
console.error('bc-county-map:', err);
});
}
document.addEventListener('DOMContentLoaded', function (){
document.querySelectorAll('.bc-county-map').forEach(renderMap);
});
})();