import{sql as o}from"drizzle-orm";const $="not_specified_version";function h(e){return o.raw(`
    CASE
      ${s(e)}
      ${S(e)}
      ${a(e)}
      ${E(e)}
    END
  `)}function s(e){return`WHEN ${e} = '${$}' THEN '!not_specified'`}function S(e){return`WHEN ${e} IS NULL OR ${e} = '' THEN '0000000000.0000000000.0000000000'`}function a(e){return`WHEN ${e} GLOB '*[^0-9.]*' THEN 'ZZZZZZZZZZ.' || ${e}`}function E(e){return`ELSE ${c(e)}`}function c(e){const r=u(e),i=N(e),t=T(e);return`PRINTF('%010d.%010d.%010d', ${r}, ${i}, ${t})`}function u(e){const r=`${e} || '.0.0'`;return`CAST(COALESCE(NULLIF(SUBSTR(${r}, 1, INSTR(${r}, '.') - 1), ''), '0') AS INTEGER)`}function N(e){const r=`${e} || '.0.0'`,i=`SUBSTR(${r}, INSTR(${r}, '.') + 1) || '.0'`;return`CAST(COALESCE(NULLIF(SUBSTR(${i}, 1, INSTR(${i}, '.') - 1), ''), '0') AS INTEGER)`}function T(e){const r=`${e} || '.0.0'`,i=`SUBSTR(${r}, INSTR(${r}, '.') + 1) || '.0'`,t=`SUBSTR(${i}, INSTR(${i}, '.') + 1) || '.0'`;return`CAST(COALESCE(NULLIF(SUBSTR(${t}, 1, INSTR(${t}, '.') - 1), ''), '0') AS INTEGER)`}function d(e){return`
    CASE
      ${s(e)}
      ${S(e)}
      ${a(e)}
      ${E(e)}
    END
  `}function l(e,r,i){const t=d("higher_version.version"),n=d(r);return o.raw(`
    NOT EXISTS (
      SELECT 1 FROM (
        -- Combine all current, non-deleted entities from both databases
        SELECT key, version, revision FROM remote.entities WHERE is_current = 1 AND is_deleted = 0
        UNION ALL
        SELECT key, version, revision FROM entities WHERE is_current = 1 AND is_deleted = 0
      ) higher_version
      WHERE higher_version.key = ${e}
        AND (
          -- Case 1: Another entity has a strictly higher version
          (${t}) > (${n})
          OR (
            -- Case 2: Same version, but another entity has a higher revision
            (${t}) = (${n})
            AND higher_version.revision > ${i}
          )
        )
    )
  `)}export{l as buildNoHigherVersionExistsFilter,h as buildSemanticVersionSortExpr};
