"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.imageUrlsFromCell = exports.normalizeFormat = exports.parseAffirmative = exports.parseEnabled = exports.parsePercent = exports.parseOptionalInteger = exports.parseNonNegativeInteger = exports.parseMoney = exports.operationalSku = exports.operationalSlug = exports.slugify = exports.sourceKey = exports.lookupKey = exports.cleanText = exports.asText = void 0;
const crypto_1 = __importDefault(require("crypto"));
const asText = (value) => {
    if (value === null || value === undefined)
        return '';
    return String(value).normalize('NFC');
};
exports.asText = asText;
const cleanText = (value) => (0, exports.asText)(value).trim().replace(/\s+/g, ' ');
exports.cleanText = cleanText;
const lookupKey = (value) => (0, exports.cleanText)(value).toLocaleLowerCase('en');
exports.lookupKey = lookupKey;
const sourceKey = (namespace, value) => crypto_1.default.createHash('sha256').update(`${namespace}\u0000${(0, exports.asText)(value)}`, 'utf8').digest('hex');
exports.sourceKey = sourceKey;
const slugify = (value) => (0, exports.cleanText)(value)
    .normalize('NFKD')
    .replace(/\p{Mark}+/gu, '')
    .toLocaleLowerCase('en')
    .replace(/[^\p{Letter}\p{Number}]+/gu, '-')
    .replace(/^-+|-+$/g, '')
    .replace(/-{2,}/g, '-');
exports.slugify = slugify;
const withSuffix = (base, suffix, maxLength) => {
    const normalizedSuffix = (0, exports.slugify)(suffix) || crypto_1.default.createHash('sha1').update(suffix).digest('hex').slice(0, 12);
    const suffixWithSeparator = `-${normalizedSuffix}`;
    const available = Math.max(1, maxLength - suffixWithSeparator.length);
    return `${base.slice(0, available).replace(/-+$/g, '')}${suffixWithSeparator}`;
};
const operationalSlug = (sourceSlugValue, title, sourceProductId, baseSlugCount) => {
    const base = (0, exports.slugify)(sourceSlugValue) || (0, exports.slugify)(title) || 'book';
    if (baseSlugCount === 1 && base.length <= 191)
        return base;
    return withSuffix(base, sourceProductId, 191);
};
exports.operationalSlug = operationalSlug;
const operationalSku = (sourceSkuValue, sourceProductId, sourceSkuCount) => {
    const raw = (0, exports.asText)(sourceSkuValue).trim();
    if (raw && sourceSkuCount === 1 && raw.length <= 191)
        return raw;
    const base = raw || 'SKU';
    const suffix = `::${sourceProductId}`;
    const available = Math.max(1, 191 - suffix.length);
    return `${base.slice(0, available)}${suffix}`;
};
exports.operationalSku = operationalSku;
const parseMoney = (value, fallback = 0) => {
    const raw = (0, exports.cleanText)(value).replace(/[,₹$£€]/g, '');
    const numberValue = Number(raw);
    if (!raw || !Number.isFinite(numberValue) || numberValue < 0) {
        return { value: fallback, warning: `Invalid money value "${(0, exports.asText)(value)}"; used ${fallback}.` };
    }
    return { value: Math.round(numberValue * 100) / 100 };
};
exports.parseMoney = parseMoney;
const parseNonNegativeInteger = (value, fallback = 0) => {
    const raw = (0, exports.cleanText)(value);
    const numberValue = Number(raw);
    if (!raw || !Number.isInteger(numberValue) || numberValue < 0) {
        return { value: fallback, warning: `Invalid non-negative integer "${(0, exports.asText)(value)}"; used ${fallback}.` };
    }
    return { value: numberValue };
};
exports.parseNonNegativeInteger = parseNonNegativeInteger;
const parseOptionalInteger = (value) => {
    const raw = (0, exports.cleanText)(value);
    if (!/^\d+$/.test(raw))
        return null;
    const parsed = Number(raw);
    return Number.isSafeInteger(parsed) ? parsed : null;
};
exports.parseOptionalInteger = parseOptionalInteger;
const parsePercent = (value) => {
    const raw = (0, exports.cleanText)(value).replace(/%$/, '');
    const parsed = Number(raw);
    if (!raw || !Number.isFinite(parsed) || parsed < 0 || parsed > 100) {
        return { value: 0, warning: `Invalid percentage "${(0, exports.asText)(value)}"; used 0%.` };
    }
    return { value: parsed };
};
exports.parsePercent = parsePercent;
const parseEnabled = (value) => (0, exports.lookupKey)(value) !== 'disabled';
exports.parseEnabled = parseEnabled;
const parseAffirmative = (value) => ['yes', 'true', '1', 'enabled', 'in stock'].includes((0, exports.lookupKey)(value));
exports.parseAffirmative = parseAffirmative;
const normalizeFormat = (binding) => {
    const value = (0, exports.lookupKey)(binding);
    if (/hard\s*(cover|back|bound|bond)/.test(value))
        return 'HARDCOVER';
    if (/paper\s*back|soft\s*cover/.test(value))
        return 'PAPERBACK';
    if (/ebook|e-book/.test(value))
        return 'EBOOK';
    if (/audio/.test(value))
        return 'AUDIOBOOK';
    return value ? 'OTHER' : 'PAPERBACK';
};
exports.normalizeFormat = normalizeFormat;
const imageUrlsFromCell = (value) => {
    const matches = (0, exports.asText)(value).match(/https?:\/\/[^\s|]+/gu) ?? [];
    return [...new Set(matches.map((url) => url.replace(/[;,]+$/g, '')))];
};
exports.imageUrlsFromCell = imageUrlsFromCell;
//# sourceMappingURL=catalog.utils.js.map