/**
 * WTE Business Card — Admin Media Upload JS
 */
'use strict';
document.addEventListener('DOMContentLoaded', function() {
	document.addEventListener('click', function(e) {
		if (!e.target.classList.contains('wte-media-select')) return;
		e.preventDefault();
		const btn = e.target;
		const input = btn.parentElement.querySelector('.wte-field-image-id');
		const frame = wp.media({ title: '画像を選択', button: { text: '選択' }, multiple: false });
		frame.on('select', function() {
			const attachment = frame.state().get('selection').first().toJSON();
			input.value = attachment.id;
			input.dispatchEvent(new Event('input', { bubbles: true }));
		});
		frame.open();
	});
});
