"use strict";

exports.__esModule = true;
exports.urlCellType = void 0;
var _types = require("../../types");
var _View = require("./View");
exports.UrlView = _View.UrlView;
var _Edit = require("./Edit");
exports.UrlEdit = _Edit.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}`;
}
const urlCellType = exports.urlCellType = (0, _types.defineCellType)({
  type: 'url',
  ViewComponent: _View.UrlView,
  EditComponent: _Edit.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