import { defineCellType } from '../../types';
import { UrlView } from './View';
import { UrlEdit } from './Edit';
export { UrlView, UrlEdit };
const URL_PROTOCOL_RE = /^https?:\/\//;
const URL_PATTERN = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$/;
/** Auto-prepend https:// on commit.
 *  - Has protocol → unchanged
 *  - No dot → append .com then prepend https://  (e.g. "example" → "https://example.com")
 *  - Has dot, no protocol → prepend https://  (e.g. "example.com" → "https://example.com")
 */
function normalizeUrlOnCommit(value) {
    if (!value)
        return value;
    if (URL_PROTOCOL_RE.test(value))
        return value;
    let normalized = value;
    if (!normalized.includes('.')) {
        normalized = `${normalized}.com`;
    }
    return `https://${normalized}`;
}
export const urlCellType = defineCellType({
    type: 'url',
    ViewComponent: UrlView,
    EditComponent: UrlEdit,
    serialize: (value) => value ?? '',
    deserialize: (raw) => raw,
    validate: (value) => {
        if (value && !URL_PATTERN.test(value)) {
            return 'Invalid URL format';
        }
        return null;
    },
    editable: true,
    clearable: true,
    // Replace the current value with the typed key (same behaviour as text/email).
    onTypeToEdit: ({ key }) => ({ value: key }),
    normalizeOnCommit: normalizeUrlOnCommit,
});
//# sourceMappingURL=index.js.map