import ButtonFields from"./fields.svelte";import ButtonCellComponent from"./button.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:"button",class:"tablesome__option--basic tablesome__option--basic-button",icon:"button",text:"Button"}),e}getColumnFormatType(e){return isUrl(e.cellValue)&&(e.format="button"),e}getColumnOptionFields(e){return"premium"===tablesome_fs.plan?("button"==e.format&&e.fields.push(ButtonFields),e):e}getColumnOnCellTypeChange(e){return"button"==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"button"==e.cellType&&(e.component=ButtonCellComponent),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("button"==e.cellType&&!e.isContentMatch){const t=e.cell.value.toLowerCase().indexOf(e.searchQuery)>=0,o="linkText"in e.cell&&e.cell.linkText&&e.cell.linkText.toLowerCase().indexOf(e.searchQuery)>=0;e.isContentMatch=t||o}return e}getSortRecords(e){if("button"==e.cellType){let t="value"in e.rowA&&e.rowA.value?e.rowA.value:"",o="value"in e.rowB&&e.rowB.value?e.rowB.value:"",l="linkText"in e.rowA&&e.rowA.linkText?e.rowA.linkText:t,n="linkText"in e.rowB&&e.rowB.linkText?e.rowB.linkText:o;e.valueA=l?l.trim().toLowerCase():"",e.valueB=n?n.trim().toLowerCase():"",e.sortType="text"}return e}getFilterRecords(e){if("button"==e.filter.format){const t="linkText"in e.cell&&e.cell.linkText?e.cell.linkText:"",o="value"in e.cell&&e.cell.value?e.cell.value:"",l=filterText(e.filter.operator.value,o,e.filter.value),n=filterText(e.filter.operator.value,t,e.filter.value);e.isContentMatch=l||n}return e}}const filterText=(e,t,o)=>(t=t.toLowerCase(),o=o.toLowerCase(),"is"==e?t==o:"is_not"==e?t!=o:"contains"==e?t.includes(o):"does_not_contain"==e?!t.includes(o):"starts_with"==e?t.startsWith(o):"ends_with"==e?t.endsWith(o):"empty"==e||"not_empty"==e?t<=o:void 0),isUrl=e=>{let t;try{t=new URL(e)}catch(e){return!1}return!0};export default LinkPlugin;