import LinkFields from"./fields.svelte";import URLCellComponent from"./url.svelte";class LinkPlugin{constructor(e){this.hooks=e,this.namespace="tablesome",this.init()}init(){this.hooks.addFilter("getColumnFormats",this.namespace,this.getColumnFormats),this.hooks.addFilter("getColumnOptionFields",this.namespace,this.getColumnOptionFields),this.hooks.addFilter("getColumnOnCellTypeChange",this.namespace,this.getColumnOnCellTypeChange),this.hooks.addFilter("getCellComponent",this.namespace,this.getCellComponent),this.hooks.addFilter("getRecords",this.namespace,this.getRecords),this.hooks.addFilter("getSearchRecords",this.namespace,this.getSearchRecords),this.hooks.addFilter("getSortRecords",this.namespace,this.getSortRecords),this.hooks.addFilter("getFilterRecords",this.namespace,this.getFilterRecords)}getColumnFormats(e){return e.push({format:"url",class:"tablesome__option--basic tablesome__option--basic-url",icon:"admin-links",text:"URL"}),e}getColumnOptionFields(e){return"url"==e.format&&e.fields.push(LinkFields),e}getColumnOnCellTypeChange(e){return"url"==e.format&&(e.no_follow="true"===e.no_follow||!0===e.no_follow||"1"===e.no_follow,e.open_in_new_tab="true"===e.open_in_new_tab||!0===e.open_in_new_tab||"1"===e.open_in_new_tab),e}getCellComponent(e){return"url"==e.cellType&&(e.component=URLCellComponent),e}getRecords(e){return"link"in e.cellData&&(e.rows[e.rowIndex].content[e.cellData.cellIndex].link=e.cellData.link),"linkText"in e.cellData&&(e.rows[e.rowIndex].content[e.cellData.cellIndex].linkText=e.cellData.linkText),e}getSearchRecords(e){if("url"==e.cellType&&!e.isContentMatch){e.cell.value=String(e.cell.value),e.cell.linkText=String(e.cell.linkText);const t=e.cell.value.toLowerCase().indexOf(e.searchQuery)>=0,l="linkText"in e.cell&&e.cell.linkText&&e.cell.linkText.toLowerCase().indexOf(e.searchQuery)>=0;e.isContentMatch=t||l}return e}getSortRecords(e){if("url"==e.cellType){let t="value"in e.rowA&&e.rowA.value?e.rowA.value:"",l="value"in e.rowB&&e.rowB.value?e.rowB.value:"",o="linkText"in e.rowA&&e.rowA.linkText?e.rowA.linkText:t,n="linkText"in e.rowB&&e.rowB.linkText?e.rowB.linkText:l;e.valueA=o?o.trim().toLowerCase():"",e.valueB=n?n.trim().toLowerCase():"",e.sortType="text"}return e}getFilterRecords(e){if("url"==e.filter.format){const t="linkText"in e.cell&&e.cell.linkText?e.cell.linkText:"",l="value"in e.cell&&e.cell.value?e.cell.value:"",o=filterText(e.filter.operator.value,l,e.filter.value),n=filterText(e.filter.operator.value,t,e.filter.value);e.isContentMatch=o||n}return e}}const filterText=(e,t,l)=>(t=t.toLowerCase(),l=l.toLowerCase(),"is"==e?t==l:"is_not"==e?t!=l:"contains"==e?t.includes(l):"does_not_contain"==e?!t.includes(l):"starts_with"==e?t.startsWith(l):"ends_with"==e?t.endsWith(l):"empty"==e||"not_empty"==e?t<=l:void 0);export default LinkPlugin;